There are many ways to make HTTP requests in Node.js. You can use either the standard HTTP/HTTPS module or one of the NPM packages like Axios, Needle, Got, SuperAgent, and node-fetch.
Before we dive into code and description, make sure that you have installed the latest LTS versions of Node.js and npm on your machine.
Make HTTP requests using Axios
The simplest and most popular way to perform an HTTP request in Node.js is using the Axios library.
Axios is a promise-based HTTP client for the browser and Node.js. It automatically transforms the response data into a JSON object.
To install Axios, run the following command in your terminal from your root directory:
$ npm install axios --save
Here is an example that makes an HTTP GET request using Axios:
const axios = require('axios')
axios
.get('https://jsonplaceholder.typicode.com/todos/1')
.then(res => {
console.log(res.data.id)
console.log(res.data.title)
})
.catch(err => {
console.log(err)
})
Since Axios supports multiple concurrent requests with axios.all
, we can concurrently call our fake REST API to get two todos information at once:
const axios = require('axios')
axios
.all([
axios.get('https://jsonplaceholder.typicode.com/todos/1'),
axios.get('https://jsonplaceholder.typicode.com/todos/2')
])
.then(
axios.spread((res1, res2) => {
console.log(res1.data.title)
console.log(res2.data.title)
})
)
.catch(err => {
console.log(err)
})
Read this article to learn more about making HTTP requests using Axios in Node.js. To use Axios in a web browser, read this tutorial instead.
Make HTTP requests using standard HTTP(S) module
Both http
and https
modules are packed in the Node.js standard library. With these modules, you can make any HTTP request without installing 3rd-party libraries, as shown below:
const https = require('https')
https
.get('https://jsonplaceholder.typicode.com/todos/1', response => {
let todo = ''
// called when a data chunk is received.
response.on('data', chunk => {
todo += chunk
})
// called when the complete response is received.
response.on('end', () => {
console.log(JSON.parse(todo).title)
})
})
.on('error', error => {
console.log('Error: ' + error.message)
})
Read this tutorial to learn about using standard http
and https
modules to make HTTP requests in Node.js.
Make HTTP requests using Got
Got is another lightweight, promise-based HTTP request library for Node.js.
Install Got from npm with the following command:
$ npm install got --save
The following example demonstrates how you can use the got
library to call our fake REST API:
const got = require('got')
got('https://jsonplaceholder.typicode.com/todos/1', { json: true })
.then(res => {
console.log(res.body.id)
console.log(res.body.title)
})
.catch(err => {
console.log(err.response.body)
})
Make HTTP requests using Node-Fetch
Node-Fetch is a light-weight HTTP request library that brings the browser's Fetch API functionality to Node.js.
You can install node-fetch from npm with the following command in your terminal:
$ npm install node-fetch --save
Just like Axios and Got, node-fetch supports promises to make an HTTP request:
const fetch = require('node-fetch')
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(res => res.json()) // expecting a json response
.then(json => {
console.log(json.id)
console.log(json.title)
})
.catch(err => {
console.log(err)
})
Make HTTP requests using Needle
Needle is a streamable HTTP client for Node.js that supports proxy, iconv, cookie, deflate, and multi-part requests.
To install Needle from npm, run the following command in your terminal:
$ npm install needle --save
The following example calls our fake REST API using Needle:
const needle = require('needle')
needle('get', 'https://jsonplaceholder.typicode.com/todos/1', { json: true })
.then(res => {
let todo = res.body
console.log(todo.id)
console.log(todo.title)
})
.catch(err => {
console.log(err)
})
Read this article to learn about making HTTP requests using Needle in Node.js.
Make HTTP requests using SuperAgent
SuperAgent is another popular HTTP library similar to Axios used for making HTTP requests in Node.js and browsers. Just like Axios, it automatically parses the response data into a JSON object.
Install SuperAgent from npm with the following command:
$ npm install superagent --save
Here is how you can use SuperAgent to call our fake REST API:
const superagent = require('superagent')
superagent
.get('https://jsonplaceholder.typicode.com/todos/1')
.then(res => {
console.log(res.body.id)
console.log(res.body.title)
})
.catch(err => {
console.log(err)
})
SuperAgent is highly extendable via plugins. There are dozens of plugins available for SuperAgent to perform different tasks such as no caching, URL prefixes and suffixes, etc. You can also write your own plugin to extend the functionality of SuperAgent.
Make HTTP requests using Request module
Request is a simplified HTTP client that is much more user-friendly as compared to the default HTTP
module. It is very popular among the community and is considered a go-to HTTP client for Node.js projects.
Note: Since 11th February 2020, request is fully deprecated. So, it is not recommended to use this package for making HTTP requests in Node.js.
Unlike the http
module, you need to install this as a dependency from Node Package Manager (npm) using the following command:
$ npm install request --save
Following is an example code snippet that uses the Request HTTP client to call our fake REST API:
const request = require('request')
request('https://jsonplaceholder.typicode.com/todos/1', { json: true }, (err, res, body) => {
if (err) {
return console.log(err)
}
console.log(body.id)
console.log(body.title)
})
Read this guide to learn more about making HTTP requests using the Request module in Node.js.
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.