In addition to Felipe's answer, I'd like to share a little bit more with my experience in using Postman.
As I need to extract some values from the responses of pm.sendRequest
and use them in making the main call (e.g. query string) and/or in the Tests section, I run the script in the Pre-request Script section and set the values in Postman environment variables.
One important point that I find out is that I must put all the code of setting variables (e.g. pm.environment.set(k, v)
) before clearTimeout(timeout)
. Otherwise, if I do it the other way round, the pm.environment.set(k, v)
code will still be run but the value of the environment variable will not be updated.
Below is an example with Postman v8.5.1
.
Main call
Expect to get TEST
from the environment variables.
GET http://google.com/{{TEST}}
Pre-request Script
Expect to set the TEST
environment variable of which the value comes from the results of multiple APIs. In this example, I just use the last value returned from Promise.all
.
// make sure you do NOT use Number.MAX_SAFE_INTEGER !!
const timeout = setTimeout(() => {}, 100000);
const promise = () => {
return new Promise((resolve, reject) => {
console.log('Calling');
pm.sendRequest('https://jsonplaceholder.typicode.com/todos/' + _.random(1, 100), (err, res) => {
console.log('run');
if (err) {
reject();
} else {
resolve(res.json());
}
});
});
}
Promise.all([
promise(),
promise(),
promise(),
promise(),
promise(),
promise(),
promise(),
promise(),
]).then(values => {
console.log('All done');
const exampleValue = values[values.length-1].id;
console.log("Last ID: " + exampleValue);
clearTimeout(timeout);
// move this line before clearTimeout to fix TEST being undefined
pm.environment.set("TEST", exampleValue);
});
Tests
Expect to print the TEST
environment variable.
// you get undefined if pm.environment.set is run after clearTimeout
// you get correct value if pm.environment.set is run before clearTimeout
console.log(pm.variables.get("TEST"));
How to test
After copying the URL and all the scripts to Postman, open up Console and click Send. Take a look at the query string of the actual URL being called (i.e. GET http://google.com/%7B%7BTEST%7D%7D
). Then rearrange the code as mentioned in the comments and click Send again. This time everything should work as expected.