Promise-based HTTP/HTTPS Client

1.0.3 · maintenance · verified Wed Apr 22

http-request-plus is a lightweight, promise-oriented, and stream-capable wrapper around Node.js's built-in `http` and `https` modules. Designed for Node.js environments (requiring `node >=14.18`), it offers a minimalistic API for making network requests, handling redirects, and processing responses as buffers, text, or JSON. The current stable version is 1.0.3, with updates appearing infrequently, suggesting a maintenance-focused cadence rather than active feature development. Its key differentiator is being a thin abstraction directly over native Node.js HTTP capabilities, providing fine-grained control and stream processing without the overhead of more feature-rich alternatives like `axios` or `node-fetch`, while still offering promise-based convenience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to make a GET request, handle potential network errors or non-2xx status codes, and retrieve the response body as text. It includes basic error handling and an example of setting headers and a timeout.

import httpRequestPlus from 'http-request-plus';

async function fetchData(url) {
  try {
    // Makes a GET request to the specified URL.
    // By default, throws an error for non-2xx status codes.
    // Options for `maxRedirects` and `bypassStatusCheck` are available.
    const response = await httpRequestPlus(url, {
      // Example of adding a custom header
      headers: {
        'User-Agent': 'http-request-plus-example/1.0'
      },
      // Timeout after 5 seconds
      timeout: 5000
    });

    if (response.statusCode >= 400) {
      console.error(`Received status code ${response.statusCode}: ${response.statusMessage}`);
      return;
    }

    // Access the response body as plain text
    const data = await response.text();
    console.log(`Successfully fetched ${url}:\n`, data.substring(0, 200) + '...'); // Log first 200 chars

  } catch (error) {
    // Catches network errors, timeouts, and non-2xx status codes (by default)
    console.error(`Error fetching ${url}:`, error.message);
    if (error.code) {
      console.error(`Error code: ${error.code}`);
    }
  }
}

fetchData('http://example.com');
// fetchData('http://nonexistent-domain-12345.com'); // Uncomment to test connection error
// fetchData('http://httpbin.org/status/404'); // Uncomment to test 404 status (will throw by default)

view raw JSON →