postmanpostman-pre-request-script

How to chain these two Postman requests


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.


Solution

  • To wait 30 sec use setTImeout in prerequest script:

    setTimeout(()=>{},30000)
    

    This will wait for 30 seconds

    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