How to Re-run a Request in Native Postman Application if Response doesn't Contain Desired Data_

Hi Postman Community,

I’m having trouble re-running a request if data doesn’t appear in the response.

This is the current scenario:

Test 1
Test 2

In Test 1, I make a POST call to send information that takes somewhere between 5-30 seconds to process.

  • Make POST call -> Receive 200 response code -> information could potentially still be processing after 200 response code is received

In Test 2, I’m making a GET call to get the information in Test 1. If I make the GET call too early, the data won’t be in the response body. What I want to do is re-run Test 2 if the data hasn’t processed yet.

  • Make GET call -> Receive response body -> If information isn’t in response body, wait a certain amount of time and re-run GET call (loop a specific number of times, e.g. 10 times, until successful, or fail test after the last attempt)

I was reading about postman.setNextRequest(“test_name”), but that seems to be deprecated. I was also reading Can I use pm.sendRequest() to send requests synchronously?, but am not sure how to translate Promises into this scenario.

What I currently have is something to this effect:

var list = JSON.parse(responseBody);
var counter = 0;
var exists_in_list = false;

/* Check for the information off the initial response body */
for(var index = 0; index < list.length; index++) {
    if(list[index].id === pm.environment.get('id_from_previous_test')) {
        pm.environment.set('processed_value', list[index].processed_value);
        exists_in_list = true;
        break;
    }
}

/* If it doesn't exist in the inital check, loop until found */
while(counter < 10 && exists_in_list === false) {
    counter += 1
    
    setTimeout(function(){}, 3000); 
    rerun_list = pm.response.json(pm.sendRequest('https://www.url.com'));
            
    for(var index = 0; index < rerun_list.length; index++) {
        if(rerun_list[index].id === pm.environment.get('id_from_previous_test')) {
            pm.environment.set('processed_value', rerun_list[index].processed_value);
            exists_in_list = true;
            break;
        }
    }
}

if(counter >= 10 && exists_in_list === false) {
    pm.test('Not found error', function() {
        throw new Error('Data not found.');
    });
}

Ideally, I would like to re-run the entire Test 2 request again to avoid the loops (the nested loops look disgusting) before moving on to Test 3. All of the pieces seem to be running asynchronously, as setTimeout doesn’t actually stop the next line from running. I tried using a custom sleep method, but that just crashed Postman.

Ideal scenario:

  1. Test 1 Runs
  2. Test 2 Runs
  3. If data is not found Test 2 runs again at a max of 10 times

However, if this is not possible, any suggestions would be much appreciated.

Thank you!

I believe you just have to include the set.nextRequest command inside the While