ItSolutionStuff.com

How to make an HTTP POST request in Node JS?

By Hardik Savani • April 29, 2021
Node JS

Hello,

This post will give you example of node js make http post request. this example will help you node js post request parameters. you can see how to call http request in node js. if you have question about node axios post request example then i will give simple example with solution. So, let's follow few step to create example of node request post body example.

i will give you two examples, using axios and request npm package for male post http request using node js. let's see both example with output as bellow:

Example 1: HTTP Post Request using Axios

Create Node App:

mkdir my-request-app

cd my-request-app

npm init

Install Axios:

npm install axios --save

server.js

const axios = require('axios');

const data = {

name: 'Hardik Savani',

job: 'Blog Writer'

};

axios.post('https://reqres.in/api/users', data)

.then((res) => {

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

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

}).catch((err) => {

console.error(err);

});

Run App

node server.js

Output

Status: 201

Body: {

name: 'Hardik Savani',

job: 'Blog Writer',

id: '246',

createdAt: '2021-04-28T10:50:47.830Z'

}

Example 2: HTTP Post Request using Request

Create Node App:

mkdir my-request-app

cd my-request-app

npm init

Install Axios:

npm install request --save

server.js

const request = require('request');

const options = {

url: 'https://reqres.in/api/users',

json: true,

body: {

name: 'Hardik Savani',

job: 'Blog Writer'

}

};

request.post(options, (err, res, body) => {

if (err) {

return console.log(err);

}

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

console.log(body);

});

Run App

node server.js

Output

Status: 201

{

name: 'Hardik Savani',

job: 'Blog Writer',

id: '650',

createdAt: '2021-04-28T10:52:11.427Z'

}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Node JS Express Form Submission Example

Read Now →

Node JS Mysql Connection Example

Read Now →

Node JS Multer Rename Uploaded File Example

Read Now →

Node JS Convert Image File to Base64 String Example

Read Now →

Node JS Resize Image Before Upload using Multer Sharp

Read Now →

Multiple File Upload in Node JS using Multer Example

Read Now →

Node js Express Multiple Image Upload using Multer Example

Read Now →

File Upload in Node JS using Multer Example

Read Now →

Node js Express Image Upload using Multer Example

Read Now →

Four Reasons Why Node.js is your Server-side Hero

Read Now →

How to Connect MySQL Database in Node JS?

Read Now →