What is requestretry?
The requestretry npm package is a wrapper around the popular request library that adds automatic retry functionality for failed HTTP requests. It allows you to specify retry strategies, including the number of retries, delay between retries, and conditions under which to retry.
What are requestretry's main functionalities?
Basic HTTP Request with Retry
This feature allows you to make an HTTP GET request with automatic retries. You can specify the maximum number of attempts, the delay between retries, and the retry strategy.
const request = require('requestretry');
request({
url: 'https://api.example.com/data',
method: 'GET',
maxAttempts: 5, // (default) try 5 times
retryDelay: 5000, // (default) wait for 5s before trying again
retryStrategy: request.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
}, function (err, response, body) {
if (err) {
console.error('Failed:', err);
} else {
console.log('Success:', body);
}
});
Custom Retry Strategy
This feature allows you to define a custom retry strategy. In this example, the request will be retried if there is an error or if the response status code is 502 (Bad Gateway).
const request = require('requestretry');
function myRetryStrategy(err, response, body) {
// retry the request if we had an error or if the response was a 'Bad Gateway'
return err || response.statusCode === 502;
}
request({
url: 'https://api.example.com/data',
method: 'GET',
maxAttempts: 3,
retryDelay: 3000,
retryStrategy: myRetryStrategy
}, function (err, response, body) {
if (err) {
console.error('Failed:', err);
} else {
console.log('Success:', body);
}
});
POST Request with Retry
This feature demonstrates how to make an HTTP POST request with automatic retries. The request includes a JSON body and will retry up to 4 times with a 2-second delay between attempts.
const request = require('requestretry');
request({
url: 'https://api.example.com/data',
method: 'POST',
json: true,
body: { key: 'value' },
maxAttempts: 4,
retryDelay: 2000,
retryStrategy: request.RetryStrategies.HTTPOrNetworkError
}, function (err, response, body) {
if (err) {
console.error('Failed:', err);
} else {
console.log('Success:', body);
}
});
Other packages similar to requestretry
axios-retry
axios-retry is a plugin for the axios HTTP client that adds retry functionality. It allows you to specify the number of retries, delay between retries, and conditions for retrying. Compared to requestretry, axios-retry is built on top of axios, which is a more modern and widely-used HTTP client than request.
got-retry
got-retry is a plugin for the got HTTP client that adds retry functionality. It provides a flexible retry mechanism with customizable options for retry count, delay, and conditions. got-retry is built on top of got, which is known for its simplicity and performance, making it a good alternative to requestretry.
superagent-retry
superagent-retry is a plugin for the superagent HTTP client that adds retry functionality. It allows you to specify the number of retries and conditions for retrying requests. superagent-retry is useful if you are already using superagent and need retry capabilities similar to those provided by requestretry.
Request-retry


When the connection fails with one of ECONNRESET
, ENOTFOUND
, ESOCKETTIMEDOUT
, ETIMEDOUT
, ECONNREFUSED
, EHOSTUNREACH
, EPIPE
or when an HTTP 5xx error occurrs, the request will automatically be re-attempted as these are often recoverable errors and will go away on retry.
Usage
Request-retry is a drop-in replacement for request but adds two new options maxAttempts
and retryDelay
.
var request = require('requestretry');
request({
url: 'https://api.domain.com/v1/a/b'
json:true,
maxAttempts: 5,
retryDelay: 5000,
retryStrategy: request.RetryStrategies.HTTPOrNetworkError
}, function(err, response, body){
});
Installation
Install with npm.
npm install --save requestretry
How to define your own retry strategy
/**
* @param {Null | Object} err
* @param {Object} response
* @return {Boolean} true if the request should be retried
*/
function myRetryStrategy(err, response){
// retry the request if we had an error or if the response was a 'Bad Gateway'
return err || response.statusCode === 502;
}
request({
url: 'https://api.domain.com/v1/a/b'
json:true,
retryStrategy: myRetryStrategy
}, function(err, response, body){
// this callback will only be called when the request succeeded or after maxAttempts or on error
});
Todo
- Tests
- Use an EventEmitter to notify retries
Changelog
[v1.2.2](#v1.2.2)
[v1.2.1](#v1.2.1)
- add support for 'write' request method by @juliendangers
[v1.2.0](#v1.2.0)
- support for user-defined retry strategies
- added
request.RetryStrategies.HTTPError
, request.RetryStrategies.NetworkError
and request.RetryStrategies.HTTPOrNetworkError
[v1.1.0](#v1.1.0)
- support for 'end', 'on', 'emit', 'once', 'setMaxListeners', 'start', 'removeListener', 'pipe' request methods by @juliendangers
[v1.0.4](#v1.0.4)
[v1.0.3](#v1.0.3)
[v1.0.2](#v1.0.2)
- upgraded
request
to 2.44.0 (19/09/2014) - callback is now optional
[v1.0.1](#v1.0.1):
[v1.0.0](#v1.0.0):
- request now yield an Request instance with a
.abort()
method.
[v0.0.1](#v0.0.1)
Copyright 2014, Francois-Guillaume Ribreau (npm@fgribreau.com)