HTTP(S) Request Retry

8.0.0 · active · verified Wed Apr 22

request-retry is a Node.js library designed to automatically re-attempt HTTP(S) requests that fail due to transient network errors or specific HTTP status codes like 5xx and 429 (Too Many Requests). It functions as a wrapper, providing a drop-in replacement API for the underlying HTTP client, but with added configurable retry logic including `maxAttempts`, `retryDelay`, and a customizable `retryStrategy`. The current stable version, 8.0.0, marks a significant shift, migrating its core dependency from the unmaintained `request` library to the more actively developed `postman-request` fork, addressing security concerns and ensuring continued functionality. This package is crucial for building robust network clients that can gracefully handle temporary service disruptions, simplifying the implementation of resilient communication patterns by abstracting away common retry mechanisms.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic promise-based HTTP GET request using request-retry, configured to retry on 5xx errors or network issues with a delay.

// First, ensure you have the necessary peer dependency installed:
// npm install requestretry postman-request

const request = require('requestretry');

async function makeRetriedRequest() {
  console.log('Initiating retriable HTTP request...');
  try {
    const response = await request({
      url: 'https://httpstat.us/503', // An endpoint that reliably returns a 503 (Service Unavailable)
      json: true, // Parse response body as JSON
      maxAttempts: 3,   // Configure up to 3 attempts
      retryDelay: 1000,  // Wait 1 second between retries
      retryStrategy: request.RetryStrategies.HTTPOrNetworkError, // Retry on 5xx status codes or network errors
      fullResponse: true // Resolve the promise with the full response object, including headers and status
    });

    console.log(`\nRequest successfully completed after ${response.attempts} attempts.`);
    console.log(`HTTP Status Code: ${response.statusCode}`);
    console.log(`Response Body: ${JSON.stringify(response.body, null, 2)}`);
  } catch (error) {
    console.error(`\nRequest failed after all attempts. Error: ${error.message}`);
    if (error.attempts) {
      console.error(`Total attempts made: ${error.attempts}`);
    }
  }
}

makeRetriedRequest();

view raw JSON →