Postman - Get only endpoint from a URL - javascript

Having this kind of endpoing:
https://hey.com/p1/m/p2
I want to get rid of the https://hey.com and get only the /p1/m/p2 part on the Pre-request Script. I know I can do it using request.url.replace(/^.*\/\/[^\/]+/, ''), getting the desired output /p1/m/p2.
Is there any other way to do it without using replace or regex? Something like request.url.pathname (which is not working, obviously).
The URL above is just an example, the endpoints and urls will vary.
Bear in mind that I'm using the Pre-request Script on the Postman environment and some things may not work.
Thank you so much.

You should be able to use the URL browser API to construct a URL object.
(new URL(request.url)).pathname
If you're using the desktop version, you can use the built-in node.js API.
var URL = require('url');
URL.parse(request.url).pathname

You can try something like this
var fullUrl = request.url.split('/');
fullUrl.splice(0,3);
var url = a.join('/');
console.log(url);

Related

How to get absolute path of a javascript?

I'm trying to find the absolute path of my javascript file (not the URL). Instead of hardcoding absolutely paths, I'd prefer using relative paths. For example:
/static/js/index.js
/static/config/1.json
/static/config/2.json
If I can get the absolute path of index.js, then I'm only ../config/ away from accessing either of the two json files.
Searching SO and the internet I either get suggestions for finding the URL, which work but won't solve my problem. Or for file paths, using windows.location.pathname but any permutation of it that I try either returns an empty string, or /.
var currentDirectory = window.location.pathname.split('/').slice(0, -1).join('/');
console.log("Curr dir: " + currentDirectory);
(Returns empty string)
var location = window.location.pathname;
var directoryPath = location.substring(0, location.lastIndexOf("/")+1);
console.log(" dirPath: " + directoryPath);
(Returns a /)
What I'm hoping for is something like:
var absolute_path = window.function.absolute.path()
Which would return say: /u/user/develop/project/server/static/js/index.js
From which I could: (pseudo code again)
var locations = absolute_path.split("js");
# location[0] = "/u/user/develop/project/server/static"
# location[1] = "js/index.js"
var config_file_locations = locations[0] + "config/";
var absolute_path_json1 = config_file_locations + "1.json"
-- EDIT --
Ok, looking at window.location it returns the URL so clearly that's not an answer here.
/static/js/index.js is an absolute path. You can tell because it starts with a /, which takes it back to the root of the web site.
There is no automatic way for a browser to tell anything about how a web server determined how it generated the content for a given URL.
All a browser knows is that it asked the server for /static/js/index.js and the server responded with some JavaScript.
The server might have read a static file, and that file might be in a directory called js and that directory might be a subdirectory of one called static … but then the server might have taken the whole URL, used it in a database query, and pulled the results from a database … or it might have proxied the request to another HTTP server on another computer on another continent.
If you want your client-side JS to know anything about the structure of the filesystem on the HTTP server, then you need to give it that information somehow. It can't get it from the browser.

email a docx file with googlescript

I'm only a beginner with using google script, so please be kind if this is a very stupid question :)
My script currently e-mails a pdf, created from a google drive file like this:
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
I'd like to also e-mail a docx file, but of course it's less easy.
I found one way to do this:
Using Google Script to send an email with a Word doc attachment
... but it seems to use an old solution which no longer works. Code:
var url = 'https://docs.google.com/feeds/';
var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=doc&format=doc&id='+copyId,
googleOAuth_('docs',url)).getBlob();
Error:
Execution failed: ReferenceError: "googleOAuth_" is not defined.
Am I using the right method but with a mistake, or is there a better method to export a docx?
Thanks very much for your help and patience!
If you are still looking for the solution, how about this answer? In order to convert Google Document to docx and export docx, please use access token. In your case, you can use ScriptApp.getOAuthToken() for the access token. So how about the following modification?
From :
var url = 'https://docs.google.com/feeds/';
var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=doc&format=doc&id='+copyId,
googleOAuth_('docs',url)).getBlob();
To :
var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + copyId + "&format=docx&access_token=" + ScriptApp.getOAuthToken();
var docx = UrlFetchApp.fetch(url).getBlob();
Note :
You can use the access token as a query parameter.
In your question, you want a docx file. But in your script, it seems that you want a doc file.
In this modification, the docx is used. If you want a doc file, please modify it.
At the modified script, docx is the blob of docx. Please use this instead of pdf of your script.
If I misunderstand your question, I'm sorry.
Updated: February 7, 2020
From January, 2020, the access token cannot be used with the query parameter like access_token=###. Ref So please use the access token to the request header instead of the query parameter. It's as follows.
var res = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});

How to get the current url with php?

Here is a problem with "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; when I using this code in a php file, which I called from a JS (with fetch or XMLHttpRequest), the output will be the the current url of this .php file!
Example I call PHP in JS (on my wordpress site):
const item = document.querySelector('.item');
fetch('path/wp-content/themes/current_theme_directory/called.php')
.then(res => res.text())
.then(responseText => item.innerHTML = responseText);
And my called.php file contain this code:
$current_url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $current_url;
My responseText in my JS this: path/wp-content/themes/current_theme_directory/called.php
In this situation how can I get the real current url, what shows my browser also?
you can get the current url using javascript, it's in a variable called window.location.href
console.log(window.location.href); will log the current url in the console.
Technically $_SERVER['HTTP_REFERER'] will have the url that loaded the PHP, but keep in mind that info is easily edited by a crafty user.

Parse Cloud Code - Get URL of PFFile

Ok this should be a relatively simple one. Apologies i am an iOS developer usually trying to understand Javascript.
I have a PFFile that is being saves into a PFObject from iOS.
I now need to send the URL of the image to a web service, which i am trying to do using an afterSave method on Cloud Code.
I have the object being fetched, and i can see the file contained within it. But i can't work out how to access the URL value.
I have tried various approaches, and i'm sure it's me not understanding how to access the value on an object, nested in another object. I've listed what i mostly tried below.
I have everything else working apart from this so help would be greatly appreciated.
Thanks
Gareth
var image = request.object.get("userPhoto")
console.log(image);
var imageURL = image.url
var imageURL = image.'url'
var imageURL = image.(url)
var imageURL = image.('url')
console.log(imageURL);
The method for accessing the URL of the Parse.File object is:
var imageURL = image.url();
Per the docs here: https://parse.com/docs/js_guide#files-retrieving

Javascript: how to append data to a file

how can i append data to a file using javascript?
i tried to use this code, but i got an error:
var fso = new ActiveXObject("Scripting.FileSystemOject");
var filepath = fso.GetFile("member.txt");
var fileObject = fso.OpenTextFile(filepath, 8);
file.WriteLine(id + "|" + pass);
fileObject.close();
the error is on var fso = new ActiveXObject("Scripting.FileSystemOject");, written: Error: Automation server can't create object
is there any other way to append the file using javascript or the way to fix this? thanks :)
EDIT:
i have doing what's written on this, and it still not working :/
I just realized these in your code:
var fileObject = fso.OpenTextFile(filepath, 8,true);
You'll need the true-argument, if the file does not exist, or you want to overwrite/append it.
var filepath = fso.GetFile("member.txt");// This won't work.
var filepath = "your_filePath"; // Use this instead
var fileObject = fso.OpenTextFile(filepath, 8, true);
OpenTextFile() needs a path as a string like "D:/test/file.txt". GetFile() returns an object, which you can see as a string (D:\test\file.txt), but it's not a string. Use also absolute paths, relative paths don't seem to work by my experience.
EDIT
Add the code below to the <head>-part of your html-file, then save locally as a hta (with file extension hta, not htm or html).
<hta:application
applicationName="MyApp"
id="myapp"
singleInstance="yes"
/>
Then run the hta-file. If you still getting an ActiveX-error, it's not supported by your OS. If this works, you haven't done all the security settings correct.
EDIT II
In this case it's not very usefull to get the path through ActiveX, you'll need to write it literal anyway. And I'm not supposed to do your homeworks, but this does the trick...
var filepath = new String(fso.GetFile("member.txt")).replace(/\\/g,'/');
And don't forget what I've said above about using absolute paths...
The 8 in the OpenTextFile function specify that you want to append to the file. Your problem comes from the security restriction of your browser. To make it work you'll have to lower the security level, which is not really recommended.
The error is thrown because there are security restrictions which donot allow the activex to run. change your security settings to allow the activex if your using internet explorer (which i think you are).
This might be useful http://windows.microsoft.com/en-US/windows/help/genuine/ie-activex
Cheers
EDIT: i have doing what's written on this, and it still not working :/
* try Restarting your browser
As pointed out in this comment
Javascript: how to append data to a file
the cause of the error Error: Automation server can't create object is the typo in the progid passed to ActiveXObject: Oject instead of Object:
var fso = new ActiveXObject("Scripting.FileSystemOject");
there is a missing b!

Categories