Search code examples
javascriptpostmanpostman-collection-runnerpostman-pre-request-scriptpostman-testcase

How to use variable from one request in another request test body in postman test body?


I want to execute 3 requests (api1, api2 and api3) one after the other. I want to make api3 request dependent on values(type:int) of api1 and api2.

In api1 request test body:

var data = pm.response.json();
var count1 = data.length;

In api2 request test body:

var data = pm.response.json();
var count2 = data.length;
if(count1 == 0 && count2 == 0){
    postman.setNextRequest(null);
}

Doing this, it throws a "ReferenceError: count1 is not defined" after firing the api2 request.

I don't want to execute the request api3 if both the count values (count1 and count2) are 0. Please help!


Solution

  • In the test of the api1 request, you need to store the value of count1 in an environment variable: pm.environment.set("count1", count1));

    Then use that environment variable in the test of api2: count1 = pm.environment.get("count1");