Postman: Is it possible to update url of postman request in the pre-requisite script

Viewed 6423

Is it possible to update url of postman request in the pre-requisite script. I want to edit the url based on dynamic environment input.

For example:

if (environment.someValue) {
    request.url = request.url + "\" + environment.someValue
    if (environment.anotherValue) {
        request.url = request.url + "\" + environment.anotherValue
    }
}

console.log(request.url);

The above code gives me prefect console log:

e.g. if url is https://someServer/someRequest, environment.someVar is x and environment.anotherVar is y the console.log(request.url) above prints:

https://someServer/someRequest/x/y

But the problem is (say if i am requesting a Get), even after logging the overridden request url, it only calls https://someServer/someRequest and does not override to https://someServer/someRequest/x/y.

Any ideas how to modify the url as asked above.

3 Answers
pm.request.url= "dynamic value"

you and update url using the above single line in postman prerequisite step

pm.request.url returns a object with fields host,path and params . But you can replace it with string.

Thanks for the tips: I used these lines of code to manipulate two values that appear in a RESTful API route:

// Ensure Payroll ID and Employee ID in request URL are correctly padded with zeros pm.request.url.path[pm.request.url.path.indexOf("{{PayrollID}}")] = data.PayrollID.toString().padStart(6, '0'); pm.request.url.path[pm.request.url.path.indexOf("{{EmployeeID}}")] = data.EmployeeID.toString().padStart(7, '0');

Related