How to make an HTTP POST request in Node JS?

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hello Brother,

This post will give you lesson of node js make http post request. this lesson 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 lesson then i will give simple lesson with solution. So, let's follow few step to create lesson of node request post body lesson.

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

Lesson 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: 'Sagor 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: 'Sagor Savani',

job: 'Blog Writer',

id: '246',

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

}

Lesson 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: 'Sagor 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: 'Sagor Savani',

job: 'Blog Writer',

id: '650',

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

}

thank you very much for following up with me.

Tags

Recent Posts

Related Posts