ReferenceError using url package

My base issue is that I want to treat the query string from a redirect header as a dictionary. The sandbox documentation says that it supports the url package.

However, when I try to new up a URL object, it says ReferenceError: URL is not defined. I can use the legacy url.parse function, but that doesn’t get me access to the URLSearchParams class.

I tried the following code in both Postman and the Nodejs playground in Katacoda. Postman of course throws the above error, but it works in Katacoda.

const url = require('url');
u = new URL('https://foo.com?bar=baz');
console.log(u);

I’m a noob at js, so I’m sure this is just some basic thing I don’t know about. I’m really hoping that this isn’t an undocumented incompatibility.

Hey,
importing and using URL with require did not work for me, but I found a workaround:

try the following to add some query paramters to a url:

const sdk = require('postman-collection');
let myUrl = new sdk.Url('https://https://accounts.eu1.gigya.com/accounts.getJWT');
myUrl.query.add({"targetUID":pm.environment.get("sap_cdc_target_uid")});
myUrl.query.add({"apiKey":pm.environment.get("sap_cdc_target_uid")});
myUrl.query.add({"userKey":pm.environment.get("sap_user_key")});

This will add Query Paramters to the query property of type (PropertyList) PropertyList - Postman Documentation

You can also access query string by index

myUrl.query.idx(0).value = "newValue";

Credits to: How can we parse a URL in a pre-request script? · Issue #3059 · postmanlabs/postman-app-support · GitHub
Documentation: Url - Postman Documentation