Axios HTTP requests in Node JS Example

By Hardik Savani May 6, 2021 Category : Node JS

Hi,

Here, i will show you how to works node js axios https request example. step by step explain how to make axios post request in node js. Here you will learn http request in nodejs js axios. i would like to show you axios post request example node js. follow bellow step for axios node js post example.

here, i will give you post, get, put and delete request using axios in node js example. so you can easily use axios https request in node js.

let's see examples bellow:

Create Node App:

mkdir my-request-app

cd my-request-app

npm init

Install Axios:

npm install axios --save

HTTP Post Request:

server.js

const axios = require('axios');

const data = {

first_name: 'Hardik',

last_name: 'Savani',

email: 'hardik@gmail.com'

};

axios.post('https://api.mywebtuts.com/api/users', data)

.then((res) => {

console.log(`Status: ${res.status}`);

console.log('Body: ', res.data);

}).catch((err) => {

console.error(err);

});

HTTP Get Request:

server.js

const axios = require('axios');

const querystring = require('querystring');

const data = {

page: 2,

};

const parameters = querystring.stringify(data);

axios.get('https://api.mywebtuts.com/api/users?'+parameters)

.then((res) => {

console.log(`Status: ${res.status}`);

console.log('Body: ', res.data);

}).catch((err) => {

console.error(err);

});

HTTP Put Request:

server.js

const axios = require('axios');

const data = {

first_name: 'Hardik',

last_name: 'Savani',

email: 'hardik@gmail.com'

};

axios.put('https://api.mywebtuts.com/users/2', data)

.then((res) => {

console.log(`Status: ${res.status}`);

console.log('Body: ', res.data);

}).catch((err) => {

console.error(err);

});

HTTP Delete Request:

server.js

const axios = require('axios');

axios.delete('https://api.mywebtuts.com/api/users/2')

.then((res) => {

console.log(`Status: ${res.status}`);

}).catch((err) => {

console.error(err);

});

i hope it can help you...

Tags :
Shares