URL to HTTP Options Converter

2.0.0 · active · verified Wed Apr 22

The `url-to-options` package is a focused utility designed to convert a WHATWG `URL` object into a plain JavaScript options object compatible with Node.js's native `http.request` and `https.request` methods. It abstracts the process of extracting host, port, protocol, auth, and path information from a standard URL representation. The current stable version is 2.0.0. Given its specific utility nature, the package is expected to have an infrequent release cadence, primarily for maintenance, bug fixes, or compatibility with newer Node.js versions or WHATWG URL specification changes. Its primary differentiator is its directness in bridging the WHATWG URL standard with Node's built-in HTTP client APIs, providing a clean way to construct request options without manual parsing.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to convert a WHATWG `URL` instance into an options object suitable for Node.js `http.request` or `https.request`, logging the resulting object.

import urlToOptions from 'url-to-options';

const rawUrl = 'https://user:pass@example.com:8443/api/data?query=test#hash';
const url = new URL(rawUrl);

// Convert the WHATWG URL object to http(s).request options
const requestOptions = urlToOptions(url);

console.log('Original URL:', rawUrl);
console.log('Generated Request Options:', requestOptions);
/* Example Output:
{
  protocol: 'https:',
  hostname: 'example.com',
  auth: 'user:pass',
  port: 8443,
  pathname: '/api/data',
  search: '?query=test',
  hash: '#hash',
  path: '/api/data?query=test'
}
*/

// To actually make a request (requires Node.js built-in modules):
// import https from 'https';
//
// const req = https.request(requestOptions, (res) => {
//   console.log(`Status: ${res.statusCode}`);
//   res.on('data', (d) => {
//     process.stdout.write(d);
//   });
// });
// req.on('error', (e) => {
//   console.error(e);
// });
// req.end();

view raw JSON →