# How to make HTTP requests in Node.js

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Jan 25, 2019, updated Nov 12, 2022. Topics: Node.js, Express.js.
Canonical URL: https://attacomsian.com/blog/http-requests-in-nodejs

> A comprehensive guide to learning about 7 different ways of making HTTP requests in Node.js using Axios, Needle, HTTP standard library, Request module, and more.

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](https://attacomsian.com/blog/install-nodejs-npm-ubuntu) 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](https://github.com/axios/axios) library.

Axios is a [promise-based](https://attacomsian.com/blog/promises-javascript) HTTP client for the browser and Node.js. It automatically transforms the response data into a [JSON](https://attacomsian.com/blog/what-is-json) object. 

To install Axios, run the following command in your terminal from your root directory:

```bash
$ npm install axios --save
```

Here is an example that makes an HTTP GET request using Axios:

```javascript
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:

```javascript
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](https://attacomsian.com/blog/http-requests-axios) to learn more about making HTTP requests using Axios in Node.js. To use Axios in a web browser, read [this tutorial](https://attacomsian.com/blog/axios-javascript) 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:

```javascript
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](https://attacomsian.com/blog/node-make-http-requests) to learn about using standard `http` and `https` modules to make HTTP requests in Node.js.

## Make HTTP requests using Got

[Got](https://github.com/sindresorhus/got) is another lightweight, promise-based HTTP request library for Node.js. 

Install Got from npm with the following command:

```bash
$ npm install got --save
```

The following example demonstrates how you can use the `got` library to call our fake REST API:

```javascript
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](https://github.com/bitinn/node-fetch) is a light-weight HTTP request library that brings the browser's [Fetch API](https://attacomsian.com/blog/javascript-fetch-api) functionality to Node.js. 

You can install node-fetch from npm with the following command in your terminal:

```bash
$ npm install node-fetch --save
```

Just like Axios and Got, node-fetch supports promises to make an HTTP request:

```javascript
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](https://github.com/tomas/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:

```bash
$ npm install needle --save
```

The following example calls our fake REST API using Needle:

```javascript
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](https://attacomsian.com/blog/http-requests-needle) to learn about making HTTP requests using Needle in Node.js.

## Make HTTP requests using SuperAgent

[SuperAgent](https://github.com/visionmedia/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:

```bash
$ npm install superagent --save
```

Here is how you can use SuperAgent to call our fake REST API:

```javascript
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](https://github.com/request/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:

```bash
$ npm install request --save
```

Following is an example code snippet that uses the Request HTTP client to call our fake REST API:

```javascript
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](https://attacomsian.com/blog/node-http-requests-using-request-module) to learn more about making HTTP requests using the Request module in Node.js.
