Postman on Status code 407 then try again

Hi all,

is there a way to make a request repeat if a status code returned is 407?

e.g.

if response.statusCode !== 200 then
repeatRequest
else
end

I guess you could do it in a couple of ways, within the request you could create a pm.sendRequest() request that fires again if it gets that response code.

Or maybe in the Collection runner with setNextRequest() to repeat the same request if it gets a 407. You could make the if condition anything to suit the context.

if(pm.response.code === 407) {
    postman.setNextRequest(pm.info.requestName)
}

Hi Danny,

i’ve tried the above and end up in a loop of the same test. Here is my code:

In PreReq Tab:

pm.environment.unset("attemptCount");
var counter = environment.counter ? _.parseInt(environment.counter) + 1 : 1;
postman.setEnvironmentVariable("attemptCount", counter);

Then in main Test Tab

pm.test("Data successfully submitted", function(){

const responseCodeCheck= (responseCode.code === 200);

const counter = parseInt(pm.environment.get('attemptCount'))

if (responseCodeCheck=== 200 && environment.counter < 1) {
          // assume this will go to next
           postman.setNextRequest();
    }

    else {

        // retry the same request

        postman.setNextRequest("SameTestThatsJustRun");

    }

});

However, on every run, it will run : SameTestThatsJustRun

I’ve console logged

responseCodeCheck - Comes back as True
responseCode.code - comes back as 200
counter - comes back as an integer and not a string

i was trying to compare a Bool to a int!

if (responseCodeCheck=== 200 && environment.counter < 1) {

should be

if (responseCodeCheck=== true && environment.counter < 1) {

Glad to see you got something working for you :trophy: - I did notice that you seem to be flipping between the older and newer sandbox syntax so that might be an area to refactor moving forwards.

Thanks for the advice @danny-dainton. Just to be clear, which is the new syntax? pm.test?

Thing’s like postman.setEnvironmentVariable, responseCode.code and accessing an environment variable like this environment.counter would be the older syntax.

A full reference can be found here:

https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/

1 Like