Want to auto-retry a failing request in postman based on a set number of max tries and with a certain amount of wait time?

Dheeraj Gambhir
1 min readMay 10, 2022

Of course, there are many ways to tackle it but I have mentioned below one of the ways where you can use the given code in the Tests tab of the postman. Unfortunately, in some of the lower environments, there can be a situation where you request fail at times due to infra issues, timeouts, etc., so in that case, this can be super helpful:

var notExpectedText = ‘Failed’;
var maxTries = 3;
var pauseBetweenTries = 5000;

if (!pm.environment.get(“tries”)) {
pm.environment.set(“tries”, 1);
}

if ((pm.expect(pm.response.text()).to.not.include(notExpectedText)) && (pm.environment.get(“tries”) < maxTries)) {
var tries = parseInt(pm.environment.get(“tries”), 10);
pm.environment.set(“tries”, tries + 1);
setTimeout(function() {}, pauseBetweenTries);
postman.setNextRequest(‘PaymentService’); //here you should give your request name that you want to retry on failure
} else {
pm.environment.unset(“tries”);

pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});

pm.test(“Get access_token”,function () {

var access_token = pm.response.json().access_token;
pm.collectionVariables.set(“access_token”, access_token);
});

}

--

--