1
votes

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.

1
What is it that you're trying to achieve, maybe someone can offer a different solution? You can use pm.request.url = "My URL" in the pre-request scripts.Danny Dainton
i need to get the path from the url, updated my post: new URL(request.url).pathnameuser468587

1 Answers

2
votes

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