Synchronous HTTP Request Utility

1.1.4 · abandoned · verified Wed Apr 22

This package provides a synchronous HTTP request utility for Node.js, built on top of `urllib` and utilizing Node.js's `spawnSync` module to achieve blocking I/O. The current stable version is 1.1.4, and it has not been updated in approximately nine years, indicating it is no longer maintained. Its primary function is to perform HTTP requests that halt the Node.js event loop until a response is received, a pattern generally considered an anti-pattern in asynchronous Node.js environments. Key differentiators include its explicit synchronous operation, contrasting with the typically non-blocking nature of Node.js. It explicitly states support only for Node.js v0.11.13+ and does not support features like response streams or custom HTTP agents, limiting its utility for modern applications requiring efficient handling of large data or complex network interactions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to make a basic synchronous GET request using `urllib-sync`. It fetches content from a URL and logs the status, headers, and a snippet of the response body. It highlights the blocking nature of the request and provides commented-out examples for POST requests and options.

const { request } = require('urllib-sync');

try {
  console.log('Making a synchronous HTTP request...');
  // This will block the Node.js event loop until the request completes.
  const res = request('https://www.google.com');
  
  console.log(`Status: ${res.status}`);
  console.log('Headers:');
  for (const key in res.headers) {
    console.log(`  ${key}: ${res.headers[key]}`);
  }
  // Note: res.data is a Buffer by default. Convert to string for display.
  console.log(`Body snippet: ${res.data.toString().substring(0, 100)}...`);

  // Example with options (e.g., method, data, timeout)
  // const postData = JSON.stringify({ key: 'value' });
  // const postRes = request('https://httpbin.org/post', {
  //   method: 'POST',
  //   data: postData,
  //   headers: { 'Content-Type': 'application/json' },
  //   timeout: 5000 // In milliseconds
  // });
  // console.log(`POST Status: ${postRes.status}, Body: ${postRes.data.toString()}`);

} catch (error) {
  console.error('Synchronous request failed:', error.message);
}

view raw JSON →