There are many ways to make an HTTP POST request in Node.js. A lot of popular open-source libraries are available for performing any kind of HTTP request.

Axios is one of such a library. It is a promise-based HTTP client that provides a simple API for making HTTP requests in vanilla JavaScript as well as in Node.js.

Using Axios, you can easily make an HTTP POST request like 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 to add 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 do 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 Content Type

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 @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.