There are many ways to make an HTTP POST request in Node.js. Many popular open-source libraries are available to perform any HTTP request.
Axios is one such library. It is a promise-based HTTP client that provides a simple API for making HTTP requests in JavaScript and Node.js.
Using Axios, you can easily make an HTTP POST request like the below:
const axios = require('axios')
const data = {
name: 'John Doe',
job: 'Content 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)
})
Axios also supports async/await syntax for performing a POST request:
const axios = require('axios')
const data = {
name: 'John Doe',
job: 'Content Writer'
}
const createUser = 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)
}
}
createUser()
Another way of making an HTTP POST request in Node.js is by using the Needle library:
const needle = require('needle')
const data = {
name: 'John Doe',
job: 'Content Writer'
}
needle('post', 'https://reqres.in/api/users', data, { json: true })
.then(res => {
console.log(`Status: ${res.statusCode}`)
console.log('Body: ', res.body)
})
.catch(err => {
console.error(err)
})
The third way is to use the Request library:
const request = require('request')
const options = {
url: 'https://reqres.in/api/users',
json: true,
body: {
name: 'John Doe',
job: 'Content Writer'
}
}
request.post(options, (err, res, body) => {
if (err) {
return console.log(err)
}
console.log(`Status: ${res.statusCode}`)
console.log(body)
})
The above three ways require adding an additional 3rd-party dependency to your project.
It is also possible to perform a POST request using Node.js native HTTPS module. But it requires doing a little extra work:
const https = require('https')
const data = JSON.stringify({
name: 'John Doe',
job: 'Content Writer'
})
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()
POST request with application/x-www-form-urlencoded
If you want to send the POST request body in application/x-www-form-urlencoded
format, the Request library is the way to go. It does not require any additional dependency for encoding data:
const request = require('request')
const options = {
url: 'https://reqres.in/api/users',
form: {
name: 'John Doe',
job: 'SEO Specialist'
}
}
request.post(options, (err, res, body) => {
if (err) {
return console.log(err)
}
console.log(JSON.parse(body))
})
Alternatively, you can use the Axios library that also supports promises for sending URL encoded form data:
const axios = require('axios')
const qs = require('querystring')
const data = {
name: 'John Doe',
job: 'Content Writer'
}
// set the headers
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
axios
.post('https://reqres.in/api/users', qs.stringify(data), config)
.then(res => {
console.log(`Status: ${res.status}`)
console.log('Body: ', res.data)
})
.catch(err => {
console.error(err)
})
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.