šÆĀ Use Case
We want to run a smoke test plan using Postman during our API deployment process across various environments. The test collection begins by checking the /health endpoint to confirm that the API is up before executing further tests. As an example, let’s take the following test collection for the Petstore API.

āļøĀ Challenge
However, we’ve observed a common issue: immediately after deployment, the /health endpoint often returns a 503 Service Unavailable status. This typically occurs because the service hasn’t fully started, even though the deployment pipeline considers it complete. As a result, our smoke tests fail prematurely, causing flaky test results and unnecessary noise in our CI/CD feedback loops.

šĀ Approach
To make this process more reliable, we want to implement a retry mechanism for the /health check. Instead of failing immediately, the request should retry a few times (with delays between attempts) until it receives a successful response. This approach allows the test to wait for the service to become available, reducing false negatives and improving the stability of our test pipeline.
To implement this, we can use Postman’s pm.execution.setNextRequest() function. This allows us to control the flow of requests within a Postman collection by specifying which request should run next.
Using this feature, we can retry the same request multiple times until it meets a success condition ā for example, receiving a 200 OK from a health check endpoint.
Below is a sample script demonstrating how to implement a simple retry mechanism:
var i = !pm.variables.get("i") ? 1 : pm.variables.get("i");
var MAX_TRIES = 10;
if (i < MAX_TRIES && pm.response.code != 200) {
console.log(`Health status : ${pm.response.code}, Test retrying count: ${i}`);
setTimeout(() =>{pm.execution.setNextRequest(pm.execution.location.current);}, 3000);
i++;
pm.variables.set("i", i);
} else {
console.log(`Health status : ${pm.response.code}`);
}
if (i >= MAX_TRIES || pm.response.status == 200) {
pm.test("API should be UP", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.status).to.eql("UP");
});
}- To introduce a delay between retries, you can wrap
pm.execution.setNextRequest()inside asetTimeout()function. This allows a pause (e.g., 3 seconds) before re-executing the same request, - To prevent the request from running in an infinite loop, itās best to introduce a
MAX_TRIESlimit. Once this limit is reached, the loop will terminateāeven if the request hasnāt returned a successful response. You can manage this using an environment variable to track the retry count across iterations.



