simple-get

4.0.1 · active · verified Wed Apr 22

simple-get is a minimalist HTTP client library for Node.js, providing the simplest way to make HTTP GET requests with essential features like HTTPS support, automatic redirect following, and gzip/deflate decompression. It focuses on being a lightweight wrapper (under 120 lines of code) around Node.js's native `http` and `https` modules, minimizing overhead. The current stable version is 4.0.1. It maintains a stable API, primarily using callback patterns, and is known for its reliability and efficiency in basic request scenarios. Key differentiators include its small footprint and stream-first approach, making it ideal for scenarios where a full-featured HTTP client is overkill, or when composing with other stream-based utilities.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic GET request using `get.concat` to fetch and parse JSON data from a public API, including error handling for network issues and non-200 responses.

const get = require('simple-get');

async function fetchData() {
  const url = 'https://jsonplaceholder.typicode.com/posts/1';
  console.log(`Fetching data from: ${url}`);

  return new Promise((resolve, reject) => {
    get.concat(url, function (err, res, data) {
      if (err) {
        console.error('Request failed:', err.message);
        return reject(err);
      }
      if (res.statusCode !== 200) {
        console.error(`Received status code ${res.statusCode}`);
        return reject(new Error(`Server responded with status ${res.statusCode}`));
      }
      try {
        const json = JSON.parse(data.toString());
        console.log('Received data successfully:');
        console.log(json);
        resolve(json);
      } catch (parseErr) {
        console.error('Failed to parse JSON:', parseErr.message);
        reject(parseErr);
      }
    });
  });
}

fetchData().catch(e => console.error('Overall error:', e.message));

view raw JSON →