Axios HTTP requests in Node JS Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Node JS Make Http Get Request with Parameters Example
- How to run node js server on https using self-signed certificate?
- Node JS Mysql Connection Example
- Node JS Resize Image Before Upload using Multer Sharp
- React Axios Post Request Example
- Laravel React JS Axios Post Request Example Tutorial
- How to Download File using Axios Vue JS?
- Vue Axios Post Request Example Tutorial
- Laravel Authenticate User in NodeJS with Socket io using JWT