Making an HTTP Request in Node.js

Jun 22, 2020

Node.js has a built-in HTTP library that lets you make HTTP requests with no outside modules. The only downside is that the API is somewhat archaic: it relies on streams, and doesn't support promises. Below is how you can make an HTTP request to httpbin.org using Node's http module:

const http = require('http');

// `res` is an instance of Node's built-in `ServerResponse` class:
// https://nodejs.org/api/http.html#http_class_http_serverresponse
const res = await new Promise(resolve => {
  http.get('http://httpbin.org/get?answer=42', resolve);
});

// A ServerResponse is a readable stream, so you need to use the
// stream-to-promise pattern to use it with async/await.
let data = await new Promise((resolve, reject) => {
  let data = '';
  res.on('data', chunk => data += chunk);
  res.on('error', err => reject(err));
  res.on('end', () => resolve(data));
});

data = JSON.parse(data);
data.args; // { answer: 42 }

There are a few nuances with Node's HTTP library that you need to know:

  1. It does not support https:// URLs. You will get a Protocol "https:" not supported. Expected "http:" error if you call http.request() with an HTTPS URL. You need to use require('https').request() instead.
  2. http.request() has a non-standard callback signature: there is no error parameter. Just http.request(url, function callback(res) {}). Because of this non-standard callback signature, you cannot use http.request() with the promisify() function.

Alternatives

Because of these rough edges in the API, most developers don't use Node.js' HTTP library for making requests. We recommend using Axios instead. Below is how you can make the above HTTP request using Axios.

const axios = require('axios');

const res = await axios.get('https://httpbin.org/get?answer=42');

res.data.args; // { answer: 42 }

Axios makes HTTP requests much cleaner than using Node.js' built-in library. Plus, since Axios requests are promises, you can handle errors using catch().


More Node Tutorials