user468587
user468587

Reputation: 5031

Postman pre-request-script URL not defined

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.

Upvotes: 3

Views: 4161

Answers (1)

Divyang Desai
Divyang Desai

Reputation: 7866

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

Upvotes: 5

Related Questions