0

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?

LAL
  • 21
  • 3

1 Answers1

2

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

lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20
  • Can you post your full request? setNextRequest does not seem to work even when running a collection – LAL Aug 22 '22 at 07:37
  • I answered this question 3 weeks ago, I didn't keep anything. My answer is kind of straighforward, you can replicate it by yourself. Request 1 return array of ids. Request 2 use each of them. – lucas-nguyen-17 Aug 22 '22 at 07:48
  • 1
    I tried, but that wasnt really enough to get going. Thanks for the "setNextRequest" though. Googling that I found a tutorial I was able to use. – LAL Aug 22 '22 at 14:00