Sometimes, we want to make a HTTP POST request in Node.js.
In this article, we’ll look at how to make a HTTP POST request in Node.js.
How to make a HTTP POST request in Node.js?
To make a HTTP POST request in Node.js, we can use the axios package.
We install it by running
npm i axios
Then we use it by writing
const axios = require('axios');
const makeRequest = async () => {
const data = {
name: 'John Doe',
job: 'Content Writer'
};
const {
data: resData,
status
} = await axios.post('https://reqres.in/api/users', data)
console.log(resData, status)
}
to call axios.post to make the request.
The data object is the JSON payload.
post returns a promise that resolves to an object with the data and status properties.
data has the response data and status has the response status code.
Conclusion
To make a HTTP POST request in Node.js, we can use the axios package.