How is an HTTP POST request made in Node.js?

Master HTTP POST in Node.js with axios, fetch, or https. From setup to handling responses, this guide walks you through each method step-by-step.

Use the https module or third-party tools like axios for POST requests. They help you send data to APIs with headers and payloads.

Problem

Making HTTP POST requests is a basic thing to do when building APIs, microservices, and web apps in Node.js. A lot of people have a hard time choosing the right way or package.

Solution

There are many ways to make HTTP POST requests in Node.js:

Example 1: https Module

const https = require('https');const data = JSON.stringify({ name: 'eSparkBiz' }); const options = { hostname: 'jsonplaceholder.typicode.com', path: '/posts', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length } }; const req = https.request(options, res => { let body = ''; res.on('data', chunk => { body += chunk; }); res.on('end', () => { console.log('Response:', body); }); }); req.on('error', error => { console.error('Error:', error); }); req.write(data); req.end();
Also Read: Top Node.js Interview Questions to Ask Before Hiring a Node.js Developer

Example 2: Axios library

npm install axios
const axios = require('axios');axios.post('https://jsonplaceholder.typicode.com/posts', { name: 'eSparkBiz' }) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });

Example 3: Fetch()

const response = await fetch('https://jsonplaceholder.typicode.com/posts', {method: 'POST',
headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'eSparkBiz' }) }); const result = await response.json(); console.log(result);

Key Takeaways

There are many ways you can send POST request in Node.js. You can either use axios or fetch() for ease and readability. Native https is most powerful but more lengthy.
Related

How to Get the Project Root Path in Node.js When working with large Node.js projects, you often need to find the root directory at runtime…

15 Oct, 2025

If you’re trying to run nodemon from the terminal and see the error: nodemon: command not found, it usually means the nodemon package is not…

10 Oct, 2025

Writing to files is a common task in Node.js, whether for logging, saving uploads or creating reports. Node.js has various methods through the built-in fs…

07 Oct, 2025