EN
Node.js - HTTP GET request using axios
0 points
In this article, we would like to show you how to make HTTP GET request using axios in Node.js.
Quick solution:
xxxxxxxxxx
1
const axios = require('axios');
2
3
axios
4
.get('/users')
5
.then(function (response) {
6
// handle success
7
console.log(response.data);
8
})
9
.catch(function (error) {
10
// handle error
11
console.log(error);
12
})
13
.then(function () {
14
// always executed
15
});
16
1. Install axios
xxxxxxxxxx
1
npm install axios
2. Import axios
using require()
xxxxxxxxxx
1
const axios = require('axios');
3. Make async GET request
xxxxxxxxxx
1
const axios = require('axios');
2
3
async function getUsers() {
4
try {
5
const response = await axios.get('http://localhost:5000/users');
6
console.log(response.data);
7
} catch (error) {
8
console.error(error);
9
}
10
}
11
12
13
// Usage example:
14
15
getUsers();