LAL
LAL

Reputation: 31

Postman tests send multiple requests

I want to make a postman request that returns a list of IDs. The returned list can contain 1 or multiple IDs. In tests section i want to use these returned IDs to make new requests. Is it possible to send requests from tests section in for loop and check their returned data BEFORE sending another request?

I tried using simple for loop and pm.sendRequest but due to async this did not work.

Is this possible using Postman?

Upvotes: 0

Views: 1341

Answers (1)

lucas-nguyen-17
lucas-nguyen-17

Reputation: 5917

You can achieve that by using postman.setNextRequest("request_name")

For example:

  • Request 1: Tab Tests Get the ids and save ids to environment
pm.environment.set("ids", JSON.stringify(pm.response.json().ids));
  • Request 2:

Tab Pre-req

let ids = JSON.parse(pm.environment.get("ids"));
if(ids.length > 0){
    let id = ids.shift();
    pm.environment.set("ids",  JSON.stringify(ids));
    pm.variables.set("id", id);
}

Tab Tests

let ids = JSON.parse(pm.environment.get("ids"));
if(ids.length > 0){
    postman.setNextRequest("Req2");
}

Result:

enter image description here

Upvotes: 2

Related Questions