/

Performing HTTP Requests in Node.js Using Axios

Performing HTTP Requests in Node.js Using Axios

Axios is a convenient JavaScript library that allows you to perform HTTP requests in Node.js. In this blog, we will explore how to use Axios to make HTTP requests in your Node.js applications.

Installation

To start using Axios, you need to install it using npm or yarn. Open your terminal and run the following command:

1
npm install axios

If you prefer using yarn, run this command instead:

1
yarn add axios

Alternatively, you can include Axios directly in your HTML page by adding this script tag:

1
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

The Axios API

Axios provides a variety of methods for different HTTP verbs, such as GET, POST, DELETE, PUT, PATCH, and OPTIONS. Here are some examples of how to use Axios to make HTTP requests:

1
2
3
4
5
6
7
axios.get('https://api.example.com/data') // GET request
axios.post('https://api.example.com/data', { foo: 'bar' }) // POST request
axios.delete('https://api.example.com/data/123') // DELETE request
axios.put('https://api.example.com/data/123', { foo: 'bar' }) // PUT request
axios.patch('https://api.example.com/data/123', { foo: 'bar' }) // PATCH request
axios.options('https://api.example.com/') // OPTIONS request
axios.head('https://api.example.com/') // HEAD request

Performing a GET Request

To make a GET request, you can use the axios.get() method. Here’s an example that retrieves a list of dog breeds from the Dog API and counts them:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const axios = require('axios')

const getBreeds = async () => {
try {
return await axios.get('https://dog.ceo/api/breeds/list/all')
} catch (error) {
console.error(error)
}
}

const countBreeds = async () => {
const breeds = await getBreeds()

if (breeds.data.message) {
console.log(`Got ${Object.entries(breeds.data.message).length} breeds`)
}
}

countBreeds()

Alternatively, you can use promises syntax instead of async/await:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const axios = require('axios')

const getBreeds = () => {
try {
return axios.get('https://dog.ceo/api/breeds/list/all')
} catch (error) {
console.error(error)
}
}

const countBreeds = async () => {
const breeds = getBreeds()
.then(response => {
if (response.data.message) {
console.log(`Got ${Object.entries(response.data.message).length} breeds`)
}
})
.catch(error => {
console.log(error)
})
}

countBreeds()

Adding Parameters to a GET Request

You can add parameters to a GET request by including them in the URL or using the params property in the options. Here are examples of both approaches:

1
2
axios.get('https://site.com/?foo=bar') // URL approach
axios.get('https://site.com/', { params: { foo: 'bar' } }) // Options approach

Performing a POST Request

Performing a POST request with Axios is similar to a GET request. You use the axios.post() method and pass the URL and an object containing the POST parameters. Here’s an example:

1
axios.post('https://site.com/', { foo: 'bar' })

By following these guidelines, you can effectively use Axios to handle various types of HTTP requests in your Node.js applications.

tags: [“http requests”, “node.js”, “Axios”, “GET requests”, “POST requests”]