Needle HTTP Client

3.5.0 · active · verified Tue Apr 21

Needle is a lightweight and performant HTTP client for Node.js, designed for various HTTP/HTTPS requests, including API interactions, file uploads, and data streaming. The current stable version is 3.5.0, and the project maintains a moderate release cadence, focusing on stability, bug fixes, and incremental feature enhancements, with major versions occurring less frequently. Key differentiators include its minimal dependency footprint, built-in support for streaming gzip, deflate, and brotli decompression (Node 10+), and automatic JSON/XML parsing. Its API is flexible, supporting traditional callbacks, modern Promises, and direct stream piping, allowing developers to choose the paradigm best suited for their application. Needle also handles essential features like Basic & Digest authentication, multipart form-data uploads, and HTTP proxy forwarding, positioning itself as an efficient alternative for quick and reliable HTTP operations in Node.js environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a Promise-based POST request for JSON data and a callback-based GET request, including basic error handling and HTTPS agent options.

import needle from 'needle';
import https from 'https'; // Required for AgentOptions if using self-signed certs

const agent = new https.Agent({
  rejectUnauthorized: process.env.NODE_ENV === 'production' // Example TLS option
});

const data = {
  username: 'testuser',
  password: process.env.TEST_PASSWORD ?? 'securepassword123'
};

needle('post', 'https://httpbin.org/post', data, { json: true, agent: agent })
  .then(response => {
    if (response.statusCode === 200) {
      console.log('Request successful:', response.body.json);
    } else {
      console.error('Request failed with status:', response.statusCode);
    }
  })
  .catch(err => {
    console.error('An error occurred during POST request:', err.message);
  });

// Example using callback style for a GET request
needle.get('https://httpbin.org/get', { agent: agent }, (error, response) => {
  if (error) {
    console.error('GET request error:', error.message);
    return;
  }
  if (response.statusCode === 200) {
    console.log('GET successful:', response.body.json);
  } else {
    console.error('GET failed with status:', response.statusCode);
  }
});

view raw JSON →