urllib for Node.js

4.9.0 · active · verified Wed Apr 22

urllib is a comprehensive HTTP client library for Node.js, designed to simplify URL interactions in complex scenarios. It provides features like basic and digest authentication, automatic redirection handling, configurable timeouts, and support for various request and response data types. The current stable version is 4.9.0, with a parallel maintenance branch for version 3 (latest 3.27.3). The library is built on top of Node.js's `undici` API, offering modern performance and adherence to web standards. It has a regular release cadence, with frequent bug fixes and minor feature additions across both major versions. Key differentiators include its robust handling of HTTP complexities often found in enterprise environments and its strong TypeScript support.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic GET request to a public API, parsing the response as JSON, and handling potential errors. It includes common options like `dataType`, `method`, `timeout`, and `headers`.

import { request } from 'urllib';

async function fetchData() {
  try {
    const { data, res } = await request('https://cnodejs.org/api/v1/topics?limit=5', {
      dataType: 'json', // Automatically parse response as JSON
      method: 'GET',
      timeout: 5000,   // 5 seconds timeout
      headers: {
        'User-Agent': 'urllib-checklist-example/1.0'
      }
    });
    console.log('Status: %s', res.status);
    console.log('Headers: %j', res.headers);
    console.log('First topic title: %s', data.data[0].title);
  } catch (error) {
    console.error('Request failed:', error.message);
  }
}

fetchData();

view raw JSON →