Postman loop over request with while loop

There is a topic here explaining looping over a request over and over with a while loop.

This seems wrong/incorrect to me. Please let me know. In this case it just looks like they are running setNextRequest(); ten times and then that request will keep looping forever.

tried this in for loop and it did not exit the loop

for(i = 0; i < 9; i++){
    postman.setNextRequest("request_name");
    console.log("Count" + i);
}

how do you loop in a request correctly?

SetNextRequest just sets the next request that will run after all of the code in the current tab has completed.

In your example code, it will just loop through the nine iterations quickly and only set the one request.

Which if its the same request with this code in, will just loop and loop for ever.

You need to set a collection variable as a counter, and then use that variable in conjunction with an IF statement to control the loop.

No need for β€œfor” or β€œwhile” loops. Just an IF statement checking the counter value. You would just need to increase the counter and have the setNextRequest in the IF statement code block.

1 Like