# Retry a failing request

**URL:** <https://community.postman.com/t/retry-a-failing-request/5934>\
**Category:** Help Hub\
**Created:** [June 17, 2019, 7:54am UTC](https://community.postman.com/t/retry-a-failing-request/5934 "2019-06-17T07:54:51Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![michal.robaszewski.j](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michal.robaszewski.j/32/5378_2.png) [@michal.robaszewski.j](https://community.postman.com/u/michal.robaszewski.j)\
**Post date:** [November 17, 2020, 3:59pm UTC](https://community.postman.com/t/retry-a-failing-request/5934/6 "2020-11-17T15:59:43Z")

</div>

Two small but life quality improvements from me.

1. Small improvement to

> [@chrissbaumann](#):
>
> ```auto
> if (!pm.environment.get("collection_tries")) {
> pm.environment.set("collection_tries", 1);
> }
> 
> ```

You can use `pm.environment.has` instead of that if( not…).

> **Standalone function code**
>
> ```auto
> // HotFix Retry function
> const statuscode = pm.collectionVariables.get("StatusCode")
> 
> if(pm.response.code != statuscode){
> let retries = pm.collectionVariables.has("retries") && pm.collectionVariables.get("retries") || 0;
> let attemptRetry = pm.collectionVariables.get("attemptRetry");
> console.warn("retries used "+retries+" of " + attemptRetry);
> 
> // time is in ms
> setTimeout(function(){}, 200);
> 
> if(retries < attemptRetry){ // Main condtion
> postman.setNextRequest(pm.info.requestName);
> pm.collectionVariables.set("retries", ++retries);
> console.warn("Increase retries "+retries);
> }
> else{        
> // Reset variable 
> pm.collectionVariables.set("retries", 0);
> console.warn("Reset retries. Failed after " + retries);
> // OR remove it 
> //pm.variables.unset("retries"); 
> postman.setNextRequest("Next_request");
> }
> }
> else{    
> pm.collectionVariables.set("retries", 0);
> console.warn("Reset retries on request Success "+ pm.collectionVariables.get("retries"));
> postman.setNextRequest("Next_request");    
> }
> 
> ```
> 
> Similar but with variables set in collection not in script.
> 
> - attemptRetry
> - retries
> - statusCode
> 
> I Use `setNextRequest` as Newman sometimes stopped execution after retry ended.

* * *

1. As a final approach you can Pack it as a global function:

> **Collection predefine helpers**
>
> ```auto
> pm.globals.set('helper', function loadhelper() {
> let helper = {};
> 
> helper.retry= (statuscode, maxRetries, Next_request, sleep=200) => {
> if(pm.response.code != statuscode){
> let retries = pm.collectionVariables.has("retries") && pm.collectionVariables.get("retries") || 0;
> console.warn("retries used "+retries+" of " + maxRetries);
> 
> // time is in ms
> setTimeout(function(){}, sleep);
> 
> if(retries < maxRetries){ // Main condtion
> postman.setNextRequest(pm.info.requestName);
> pm.collectionVariables.set("retries", ++retries);
> console.warn("Increase retries "+retries);
> }
> else{        
> // Reset variable
> pm.collectionVariables.set("retries", 0);
> console.warn("Reset retries. Failed after " + retries);
> // OR remove it
> //pm.variables.unset("retries");
> postman.setNextRequest(Next_request);
> }
> }
> else{    
> pm.collectionVariables.set("retries", 0);
> console.warn("Reset retries on request Success "+ pm.collectionVariables.get("retries"));
> postman.setNextRequest(Next_request);    
> }
> };
> //// and next Global helper function. 
> //// helper.testModelXYZ () => {};
> 
> return helper;
> } + '; loadhelper();');
> 
> ```

> **Two liner usage:**
>
> ```auto
> // Use helper function from directory
> const helper = eval(pm.globals.get("helper"));
> pm.test("Make some retry- Generally test name", helper.retry(200,3,"some next req", 5000));
> 
> ```

You can store it in CollectionVariables but If you use version control mechanism it is annoying to see this variable changed in almost all PR.  
Put this on some `Make stuff` request number one in collection.  
Or on the directory level of the collection but this may be expensive as this JS will be executed too frequently in my humble opinion. You can always wrap it with `pm.environment.has` and skip if it is already available.

Predefined Helpers function is the way for me for large repeated part of JS code in Postman.

Take it and share it as it make retries as part of your “Swiss knife toolkit”

---

_[View the full topic](https://community.postman.com/t/retry-a-failing-request/5934)._
