Postman pre-request-script URL not defined - postman

I have a pre-request-script in Postman which need to create URL:
var uri = new URL(request.url).pathname;
console.log("uri:" + uri);
Which failed with error: URL is not defined. I searched around and tried all different ways:
//const url = require('url');
//const URL = require('url').URL;
None of them work. I check node version and npm, it showed those are on installed
node -v
-bash: node: command not found
npm -v
-bash: npm: command not found
Do I have to install node for this to work?
I also run the code in chrome developer tool console, same result as undefined:
var uri = new URL(request.url).pathname
undefined
But in the same script I also use CryptoJS, which doesn't require any import, it just works.
I'm using macOS Mojave 10.14.6, and POSTMAN 7.21.2 app, not chrome extension.

Postman has support for APIs, some of them are pre-included. CryptoJS, for example, is pre-included, hence you'd not need to add explicitly. The pre-request script has also support for several node modules, to make them work, Postman documentation stated:
In order to use a library, simply call the require function and pass
the module name as a parameter and assign the return of the function
to a variable.
So, in your case, it should be something like:
const url = require('url');
var pathName = url.parse(request.url).pathname;
console.log(pathName);
Detailed documentation: Postman Sandbox API reference

Related

Uncaught ReferenceError: require is not defined when " require('amadeus') "

I have followed the Amadeus steps to set up the Amadeus account and installed the Node SDK using "npm install amadeus --save".
I wrote this function:
function fetchInfo(){
let Amadeus = require('amadeus');
let amadeus = new Amadeus({
clientId: 'key',
clientSecret: 'secret'
});
}
in Javascript on WebStorm. Whenever I try to test this function on Chrome, it gives out "Uncaught ReferenceError: require is not defined". I also notice that there is no "color" on "require", looks like my WebStorm does not recognize this function. Am I missing any libraries?
Thank you!!!
That happens because require does not exist in the browser. You can try to use something like browserify or requirejs, but we don't recommend to do so, since you are exposing your credentials to the user. The SDK is designed to run on server-side only.

Postman Script to set Environment variable

I am creating a postman collection for one of API integration with our Service. Here we are using OAuth1.0 for authentication. I want to set the oauth init response oauth_token, oauth_token_secret into postman environment variables so that I can access them in further requests.
The response is in below format not a JSON.
oauth_token=oauth_token_value&oauth_token_secret=oauth_token_secret_value&oauth_callback_confirmed=true
I tried below script:
var output = require('querystring').parse(Response.text);
postman.setGlobalVariable("oauth_token", output.oauth_token);
postman.setGlobalVariable("oauth_token_secret", output.oauth_token_secret);
Can some one help me to set tokens into postman environment variables please.
Note: I am using chrome plugin Version 5.5.4, not native app.
In the chrome extension you need to use postman.setEnvironmentVariable():
postman.setEnvironmentVariable("oauth_token", output.oauth_token);
postman.setEnvironmentVariable("oauth_token_secret", output.oauth_token_secret);
For that you need to have an environment created and selected (no need to add any variable manually). How to do that is described here: https://learning.postman.com/docs/sending-requests/managing-environments/#creating-environments

Postman: How do you delete cookies in the pre-request script?

All the postman cookie-management answers I've seen refer to either the browser extension (open chrome, delete cookies viz interceptor etc) or with the app, using the UI to manually manage cookies.
I would like to delete certain cookies in my pre-request code as part of scripting my API tests. (delete them programmatically)
The Sandobx API docs mention pm.cookies so I tried
if (pm.cookies !== null) {
console.log("cookies!");
console.log(pm.cookies);
}
But the pm.cookies array is empty. Yet in the console, the GET call then passes a cookie.
There's also postman.getResponseCookies, which is null (I assume because we're in the pre-request section, not in the test section)
One answer suggested calling the postman-echo service to delete the cookie. I haven't investigated this yet, but it doesn't feel right.
new version now supports that since 2019/08, see more examples here: Delete cookies programmatically · Issue #3312 · postmanlabs/postman-app-support
Prerequisite
Cookie domains to be given programatic access must be whitelisted.
clear all cookies
const jar = pm.cookies.jar();
jar.clear(pm.request.url, function (error) {
// error - <Error>
});
get all cookies
const jar = pm.cookies.jar();
jar.getAll('http://example.com', function (error, cookies) {
// error - <Error>
// cookies - <PostmanCookieList>
// PostmanCookieList: https://www.postmanlabs.com/postman-collection/CookieList.html
});
get specific cookie
const jar = pm.cookies.jar();
jar.get('http://example.com', 'token', function (error, value) {
// error - <Error>
// value - <String>
});
According to the documentation pm API reference the pm.cookie API is only for the Tests tab, not for the Pre-request Script.
The following items are available in TEST SCRIPTS only.
pm.cookies
...
It seems that you will have to stick with this method : Interceptor Blog post
I know this is a very late answer, but for my case where I didn't want to use the cookies to start the execution of the collection, I just needed to uncheck the option "Save cookies after the collection run" and check the option "Run collection without using stored cookies" on the Runner panel.
And then if I want to manage the cookies on my own, I created a first request on the collection and used the Tests tab just to collect the cookies that I wanted and saved them on a variable.
pm.environment.set('cookie', pm.cookies.get('csrftoken'))
pm.environment.set('sessionid', pm.cookies.get('sessionid'))

cURL vs Requests (SSL issue?)

All, I'm getting some strange behavior trying to use requests for an https call into the gitub api:
print(requests.get('https://api.github.com/gists/bbc56a82f359eccd4bd6').text)
The output looks like printing a binary file (no point in pasting the garbled output here).
An equivalent cURL call ("curl https://api.github.com/gists/bbc56a82f359eccd4bd6") results in the JSON response I'm expecting.
All this started after fixing a pip issue (InsecurePlatformWarning), where a few security-related packages were installed. This fix is required for users of python<2.7.9. I'm on 2.7.3 as it was recommended on some sites not to touch the python build on debian (for dependency-breaking issues).
Note that the issue that i'm having breaks functionality for e.g. github3py python API wrapper, etc.
Is anyone else seeing issues with requests after the upgrade? Any fixes?
This URL clearly responds differently depending on user-agent. I could make the curl command line response differ by simply adding -A moo/1.
You can probably get a curl-like response with Requests from this by using a curl like user-agent.
Or even better: just ask github or read up on their API.
I'm not seeing that behaviour here:
>>> import requests
>>> print(requests.get('https://api.github.com/gists/bbc56a82f359eccd4bd6').text)
Returns a JSON string. You could try debugging this further by changing the User-Agent of your request call to be that of cURL:
headers = {
'User-Agent': 'curl/7.38.0',
}
url = 'https://api.github.com/gists/bbc56a82f359eccd4bd6'
response = requests.get(url, headers=headers)

Microsoft Azure appending extra query string to urls with query strings

In deploying a version of the Django website I'm working on to Microsoft's Azure service, I added a page which takes a query string like
http://<my_site_name>.azurewebsites.net/security/user/?username=<some_username>&password=<some_password>
However, I was getting 404 responses to this URL. So I turned on Django's Debug flag and the page I get returned said:
Page not found (404)
Request Method: GET
Request URL: http://<my_site_name>.azurewebsites.net/security/user/?username=<some_username>&password=<some_password>?username=<some_username>&password=<some_password>
Using the `URLconf` defined in `<my_project_name>.urls`, Django tried these URL patterns, in this order:
^$
^security/ ^user/$
^account/
^admin/
^api/
The current URL, `security/user/?username=<some_username>&password=<some_password>`, didn't match any of these.
So it seems to be appending the query string onto the end of the url that already has the same query string. I have the site running on my local machine and on an iis server on my internal network which I'm using for staging before pushing to Azure. Neither of these site deployments do this, so this seems to be something specific to Azure.
Is there something I need to set in the Azure website management interface to prevent it from modifying URLs with query strings? Is there something I'm doing wrong with regards to using query strings with Azure?
In speaking to the providers of wfastcgi.py they told me it may be an issue with wfastcgi.py that is causing this problem. While they look into it they gave me a work around that fixes the issue.
Download the latest copy of wfastcgi.py from http://pytools.codeplex.com/releases
In that file find this part of the code:
if 'HTTP_X_ORIGINAL_URL' in record.params:
# We've been re-written for shared FastCGI hosting, send the original URL as the PATH_INFO.
record.params['PATH_INFO'] = record.params['HTTP_X_ORIGINAL_URL']
And add right below it (still part of the if block):
# PATH_INFO is not supposed to include the query parameters, so remove them
record.params['PATH_INFO'] = record.params['PATH_INFO'].split('?')[0]
Then, upload/deploy this modified file to the Azure site (either use the ftp to put it somewhere or add it to your site deployment. I'm deploying it so that if I need to modify it further its versioned and backed up.
In the Azure management page for the site, go to the site's configure page and change the handler mapping to point to the modified wfastcgi.py file and save the configuration.
i.e. my handler used to be the default D:\python27\scripts\wfastcgi.py. Since I deployed my modified file, the handler path is now: D:\home\site\wwwroot\wfastcgi.py
I also restarted the site, but you may not have to.
This modified script should now strip the query string from PATH_INFO, and urls with query strings should work. I'll be using this until I hear from the wfastcgi.py devs that the default wfastcgi.py file in the Python27 install has been fixed/replaced.