Tuesday, February 25, 2020

POSTMAN - How to save a response field as a variable

In the "Tests" tab, use the code below:

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("myId", jsonData.id);

It will save the id field in the JSON response payload to the variable myId. The variable can be used in another request this way: {{myId}}

Friday, February 21, 2020

POSTMAN - How to use random number in the request

Just use the variable randomInt, for example:
In the request:
{
    "id": "{{$randomInt}}"
}

In the pre-request Script:
postman.setEnvironmentVariable("myId", "MY_ID_"+ _.random(1,99999));
console.log(postman.getEnvironmentVariable("myId"));
Read this link for more information: https://learning.postman.com/docs/postman/variables-and-environments/variables/