1

I'm new to Postman and I am trying to automate usage of these two requests. The first one is a POST request which returns a JSON with a single key:value pair ("id") in it The second one is a POST request which just returns 200 OK

So far I've managed to save "id" from the 1st request's response to an environment variable.

However, I still need to do the following: After sending 1st request, wait about 30 seconds, put "id" from first request in the URL of 2nd request, then send 2nd request.

damnatus
  • 23
  • 3

2 Answers2

1

To wait 30 sec use setTImeout in prerequest script:

setTimeout(()=>{},30000)

This will wait for 30000 second

Now to send the id url you can directly add it to url as {{id}} or in prerequest script add :

pm.request.addQueryParams({key:"id",value:pm.variables.get("id")})

if you want to run the request again and again till you get 200:

add this to test section of request 2

if (pm.response.code !== 200) {
    setTimeout(()=>{postman.setNextRequest(pm.info.requestName)},5000)
    

}

Note: there is a automatic delay between request option in postman you can use that also.

Also setNextRequest works only if you run using newman or colection runner

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • Thank you, it works. Now can I retry 2nd request a couple of times (with like 5 second pause inbetween) until I get a 200 OK response? – damnatus Feb 04 '21 at 07:30
  • @damnatus are you doing this manually or through automation ? – PDHide Feb 04 '21 at 07:58
  • I am using Collection Runner but if it's possible through manually running the 1st request I will be glad to learn it – damnatus Feb 04 '21 at 08:10
  • @damnatus first request works for both collection runner and manuall but sending again and again request 2 automatically , you should use pm.sendrequest or collection runner . I updated the answer you can try that – PDHide Feb 04 '21 at 08:13
  • How should I replace the request name in "pm.info.requestName" ? My 2nd request is named just "request 2" – damnatus Feb 04 '21 at 08:13
  • @damnatus you dont' have to replace it when you keep it in reqeust2 test section pm.info.requestname returns the name of the request itself which is request2 – PDHide Feb 04 '21 at 08:15
  • 1
    thank you (I just realized it just means "use THIS request" haha) – damnatus Feb 04 '21 at 08:16
  • could you accept the answer by clicking the tick sign if it helped – PDHide Feb 04 '21 at 08:17
0

In the below image, you can find a delay option before you try to run postman. This option is used to add a delay between requests running in the runner.

You can check my video to learn more about postman runner and request chaining. Postman Runner and Request Chaining Explained in Detail enter image description here

VedantK
  • 9,728
  • 7
  • 66
  • 71