# Postman on Status code 407 then try again

**URL:** <https://community.postman.com/t/postman-on-status-code-407-then-try-again/14819>\
**Category:** Help Hub\
**Created:** [August 7, 2020, 2:53pm UTC](https://community.postman.com/t/postman-on-status-code-407-then-try-again/14819 "2020-08-07T14:53:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JBird20](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@JBird20](https://community.postman.com/u/JBird20)\
**Post date:** [August 7, 2020, 2:53pm UTC](https://community.postman.com/t/postman-on-status-code-407-then-try-again/14819/1 "2020-08-07T14:53:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 7, 2020, 3:26pm UTC](https://community.postman.com/t/postman-on-status-code-407-then-try-again/14819/2 "2020-08-07T15:26:55Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![JBird20](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@JBird20](https://community.postman.com/u/JBird20)\
**Post date:** [August 10, 2020, 7:59am UTC](https://community.postman.com/t/postman-on-status-code-407-then-try-again/14819/3 "2020-08-10T07:59:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JBird20](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@JBird20](https://community.postman.com/u/JBird20)\
**Post date:** [August 10, 2020, 8:05am UTC](https://community.postman.com/t/postman-on-status-code-407-then-try-again/14819/4 "2020-08-10T08:05:16Z")

</div>

i was trying to compare a Bool to a int!

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

should be

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

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 10, 2020, 9:50am UTC](https://community.postman.com/t/postman-on-status-code-407-then-try-again/14819/5 "2020-08-10T09:50:24Z")

</div>

Glad to see you got something working for you 🏆 - 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.

---

<div class="post-metadata">

**Author:** ![JBird20](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@JBird20](https://community.postman.com/u/JBird20)\
**Post date:** [August 10, 2020, 10:02am UTC](https://community.postman.com/t/postman-on-status-code-407-then-try-again/14819/6 "2020-08-10T10:02:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 10, 2020, 10:24am UTC](https://community.postman.com/t/postman-on-status-code-407-then-try-again/14819/7 "2020-08-10T10:24:44Z")

</div>

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/](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/)
