This content originally appeared on CodeSource.io and was authored by Md Niaz Rahman Khan
In this article, you will learn about how to make an HTTP POST request by using NodeJS
HTTP stands for Hyper-Text Transfer Protocol and you can make different types of requests for performing different actions. Making a POST request is one of them. There are many different ways to make a POST request in NodeJS. One of the popular approaches is to use an open-source library like Axios
In Axios, you will get a simple API to make an HTTP request. It is basically a promise-based HTTP client and you may use it in vanilla JavaScript and NodeJS. To make an HTTP POST request in NodeJS with the help of Axios, we need to install Axios first. See the below command for installing Axios:
npm install axios
// or
npm i axios
After installing Axios into our NodeJS program, let’s use it and see the below code example of making an HTTP POST request:
const axios = require('axios');
const data = {
name: 'Deven Rathore',
age : 21
};
axios.post('http://dummy.restapiexample.com/api/v1/create', data)
.then((res) => {
console.log(`Status: ${res.status}`);
console.log('Student Info: ', res.data);
}).catch((err) => {
console.error(err);
});
/*
Output:
Status: 200
Student Info: {
status: 'success',
data: { name: 'Deven Rathore', age: 21, id: 9322 },
message: 'Successfully! Record has been added.'
}
*/
Here, we simply make a POST request in a dummy rest API and you can see that our data has been posted successfully also it generated a random id as well as status code and a successful message.
You may also use the asynchronous function for making an HTTP POST request with Axios. Let’s see an example of doing it in the below section:
const axios = require('axios');
const data = {
name: 'Deven Rathore',
age : 21
};
const createStudent = async () => {
try {
const res = await axios.post('https://reqres.in/api/users', data);
console.log(`Status: ${res.status}`);
console.log('Body: ', res.data);
} catch (err) {
console.error(err);
}
};
createStudent();
/*
Output:
Status: 201
Body: {
name: 'Deven Rathore',
age: 21,
id: '192'
}
*/
Another way of making an HTTP POST request is to use NodeJS built-in HTTP module. Though it is lengthy and you need to write much more code but let’s see how you can do it with NodeJS module
const https = require('https');
const data = JSON.stringify({
name: 'Deven Rathore',
age : 21
});
const options = {
hostname: 'reqres.in',
path: '/api/users',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = https.request(options, (res) => {
let data = '';
console.log('Status Code:', res.statusCode);
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('Body: ', JSON.parse(data));
});
}).on("error", (err) => {
console.log("Error: ", err.message);
});
req.write(data);
req.end();
/*
Output:
Status Code: 201
Body: {
name: 'Deven Rathore',
age: 21,
id: '447'
}
*/
These are the popular ways of creating an HTTP POST request in NodeJS.
This content originally appeared on CodeSource.io and was authored by Md Niaz Rahman Khan
Md Niaz Rahman Khan | Sciencx (2022-01-24T17:37:33+00:00) How to make an HTTP POST request with NodeJS. Retrieved from https://www.scien.cx/2022/01/24/how-to-make-an-http-post-request-with-nodejs/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.