Requisition HTTP Client

1.7.0 · abandoned · verified Wed Apr 22

Requisition is a lightweight, fluent HTTP client designed specifically for Node.js, with a strong emphasis on ES6/ES7 `async/await` patterns. It provides a minimal API for making HTTP requests (GET, POST, etc.) and offers utilities for handling responses, such as parsing JSON, reading as text or buffer, and saving to a file. Unlike browser-compatible alternatives like Axios, Requisition focuses solely on the Node.js environment, foregoing large options objects in favor of a chainable, method-based interface. The package is currently at version 1.7.0, with its last update nearly seven years ago (June 2017), indicating it is no longer actively maintained and should be considered abandoned. Due to its age, it targets older Node.js versions and exclusively uses CommonJS modules.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to make a GET request to fetch user data, parse a JSON response, handle different HTTP statuses, and conditionally save a related file to disk, all using `async/await` with `requisition`.

const req = require('requisition');

async function fetchAndSaveUser(userId) {
  try {
    console.log(`Fetching user ${userId}...`);
    const userResponse = await req(`/users/${userId}.json`);

    if (userResponse.status !== 200) {
      console.error(`Error fetching user ${userId}: Status ${userResponse.status}`);
      await userResponse.dump(); // Consume unhandled body to prevent memory leaks
      return;
    }

    const userData = await userResponse.json();
    console.log(`User ${userId} data:`, userData);

    // Example: Save an image if the user had one (hypothetical)
    if (userData.profileImage) {
      console.log(`Fetching profile image for user ${userId}...`);
      const imageResponse = await req(userData.profileImage);
      if (imageResponse.status === 200) {
        const savedPath = await imageResponse.saveTo(`/tmp/user_${userId}_profile.png`);
        console.log(`Profile image saved to: ${savedPath}`);
      } else {
        console.warn(`Could not fetch profile image for user ${userId}: Status ${imageResponse.status}`);
        await imageResponse.dump();
      }
    }

  } catch (error) {
    console.error('An error occurred during the request:', error.message);
  }
}

// To run this example, you'd need a simple local server
// For demonstration purposes, assume '/users/123.json' and '/users/123/image.png' exist

// Example usage (in an async IIFE or main function):
(async () => {
  await fetchAndSaveUser(123);
  await fetchAndSaveUser(456);
})();

view raw JSON →