Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

postman

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.

like image 428
overflower Avatar asked Sep 18 '17 04:09

overflower


People also ask

What is the difference between pre-request script and Tests in Postman?

The pre-request script is the entry point for request execution in Postman. If there is any script/logic added as a part of the pre-request script that gets executed first following which the actual request execution takes place and once the response is received, the tests or the post request scripts get executed.


1 Answers

if your url in your request is set as a global, it should work. ie. I have a get request :

GET http://{{myurl}}/etc. with myurl set as a global variable

In my prerequest script I do pm.globals.set("myurl", <new url>);

when I launch my request, it tries to do the GET request on my new url.

So it is possible to do it but you have to use global or environment variables to dynamically update your request:

set your 'someRequest' as a global that you can update in your prescript (instead of request.url), then it will be interpreted when you launch your request

https://someServer/{{someRequest}}
like image 62
A.Joly Avatar answered Sep 22 '22 05:09

A.Joly