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:
- It does not support
https://
URLs. You will get aProtocol "https:" not supported. Expected "http:"
error if you callhttp.request()
with an HTTPS URL. You need to userequire('https').request()
instead. http.request()
has a non-standard callback signature: there is no error parameter. Justhttp.request(url, function callback(res) {})
. Because of this non-standard callback signature, you cannot usehttp.request()
with thepromisify()
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()
.
Did you find this tutorial useful? Say thanks by starring our repo on GitHub!