Unable to run the same request multiple times

Can anyone tell me why this code won’t run the same GET request multiple times based on the ‘if’ condition? It just runs once and then stops. This request is part of a workflow. I am unable to find anything wrong with this code.

const maxNumberOfTries = 3; // your max number of tries

const sleepBetweenTries = 6000; // your interval between attempts

if (!pm.environment.get("tries")) {

    pm.environment.set("tries", 1);

}

console.log("Retries " + pm.environment.get("tries") + " of " + maxNumberOfTries);

if (pm.environment.get("tries") < maxNumberOfTries) {

    const tries = parseInt(pm.environment.get("tries"), 10);

    pm.environment.set("tries", tries + 1);

    setTimeout(function () {}, sleepBetweenTries);

    postman.setNextRequest(pm.info.requestName);

    } else {

  

    pm.test("Status code is 200", function () {

        pm.response.to.have.status(200);

    });    

}

Hey @oasis, how are you running the collection?

This should work fine in Runner https://learning.postman.com/docs/running-collections/intro-to-collection-runs/ or by using newman on CLI .

Also, for setTimeout() - you would need to explicitly use setInterval for it to work, check out these resources:

Cheers

1 Like

Hi @amit, I have 4 requests in a folder. I choose the folder in the collection runner. This request is the 3rd one. All prev requests run fine and when it comes to this one, it gets executed once(the very first) as it was behaving previously. And since I have declared setTimeout() explicitly it seems to be waiting there for ever now.

Though it prints “I am here”, it doesn’t seem to be running postman.setNextRequest(pm.infor.requestName). I can’t see it running at all in the console.

This is all I have in the console:-

Hi @amit, I think I have found the issue. I had another setNextRequest to run the 4th request as you can see in the above screenshot. I moved that to the ‘else’ and it runs fine now. I fail to understand how setNextRequest works sometimes:)

Thanks for your help.

Hi,
I see that you have found a solution already. If you need more examples about retries you can check those code snippets here

Cheers :slight_smile:

1 Like

Thanks @michal.robaszewski.j, that’s really useful

Hi @oasis and @michal.robaszewski.j I am trying to implement these scripts on my projects with out success i.e. it still stops the collection run at first error. Two things are unclear to me: where should I place this code (‘pre-request Script’ or ‘Tests’ section) and if it works if I don’t get any response.
I have several concatenated POSTs to execute and test but sometimes I get ‘Could not get response Error: read ECONNRESET’
Any help is appreciated!

@cezars2 , Where’s the script? Also, explaining in detail could get you an answer sooner

Hello. sorry for the delayed reply. The scripts I used are the one from you from above and the one by @michal.robaszewski.j from here. I placed the scripts in the ‘pre-request Script’ section without any success.

The problem is that the server does not give any response, in Postman it looks like this

In this case I am wondering if there is a method to automatically send again the POST request.

I have a collection with several POST requests and I would like to run the collection’s requests without interruptions, yet when I get this issue the runner stops, it does not go to the next request; if I configured the runner with more Iterations, It will jump to the next Iteration which is not the desired behavior. The desired behavior is to retry the same request until I receive a response, any response.

Hi, This message is rather not caused by the scripts nor the tests.
You need to test the connection in another tool like curl to that server. It can be one of many network related issues Like DNS , host or server struggling to establish connection and more.
You can add the retry script on Pre-req part of the script or collection and invoke it on test tab.

As in example if you add the Main code in Pre-req then you add to test tab

const helper = eval(pm.globals.get("helper"));
pm.test("Make some retry- Generally test name", helper.retry(200,3,"some next req", 5000));

And if you need more retries change 3 to sth like 10 but you may decrease the time form 5s(5000) to sth like 1000 (1sec)
And the name of next Request is crucial for Runner to continue after success in response.

To be honest for this kind of errors. you should seek for fix rather than applying retry policy. Use sth like Wireshark and check how networks fly around but this is strictly depended on technology that you are testing.