I have to create an auth hash for a web api call in my Postman pre-request script. My collection stores the service URL in a collection-level variable called baseUrl
. The value of this variable is http://api-server.local
Unfortunately, the baseUrl
variable isn't expanded when I try to directly use it from pm.request.url
. I get back a value of {{baseUrl}}
in the host
property of pm.request.url
in my pre-request script.
My guess is that the evaluation happens later in the request creation pipeline.
So, I have added a new variable to my collection called baseUrlHost
with the value set to api-server.local
. I intended to use this new variable to set the host correctly in my auth hash.
Rather than do a whole lot of string replacement, I would prefer to create a new Url
object. As per the Postman documentation, the Url Node.js module is available. However, when I create a Url
object, it is mostly full of null properties.
var Url = require('url').Url;
//another variation of the above...
//const {Url } = require('url')
var collectionBaseUrl = pm.collectionVariables.get("baseUrl")
console.log('base url from collection: ', collectionBaseUrl);
//base url from collection should be : http://api-server.local
var baseUrl = new Url(collectionBaseUrl);
console.log('base url ctor: ', baseUrl);
Here's the output...
base url ctor:
{protocol: null, slashes: null, auth: null…}
protocol: null
slashes: null
auth: null
host: null
port: null
hostname: null
hash: null
search: null
query: null
pathname: null
path: null
href: null
What should I do differently to correctly initialise the Url object?