How To Send A Post Request Using Node.js

You can send a POST request from your Node.js app using the ‘http’ library, which is bundled with Node.js by default. This has many potential use cases, as HTTP POST requests are used to facilitate  many of the activities we do online such as entering data on a form for submission or other forms of data entry.

In order to send a POST request in Node.js, you need to first import the ‘http’ module (this is one many modules available for creating HTTP requests). The second step is to determine which server you need to send the Node.js POST request to as well as the correct port and path/route. These will be defined in the ‘urlparams’ object as shown below.

An HTTP ‘POST’ request differs from an HTTP ‘GET’ request in that it is meant for posting (saving) data to a server, while GET requests are for retrieving data from a server. POST requests are also more private than GET requests as the request parameters are not all in the URL (unlike a GET request).

var http = require('http');

var urlparams = {
    host: '', //No need to include 'http://' or 'www.'
    port: 80,
    path: '/test',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json', //Specifying to the server that we are sending JSON 

function SendRequest(datatosend) {
    function OnResponse(response) {
        var data = '';

        response.on('data', function(chunk) {
            data += chunk; //Append each chunk of data received to this variable.
        response.on('end', function() {
            console.log(data); //Display the server's response, if any.

    var request = http.request(urlparams, OnResponse); //Create a request object.

    request.write(datatosend); //Send off the request.
    request.end(); //End the request.

SendRequest("{testfield: 'Boop'"); //Execute the function the request is in.

On the server side, you should have received a “{testfield: ‘Boop’}” message in the request body, which is the string that we sent in our Node.js POST request. In this case, the string contains a JSON object.

Tested on Node.js versions:

  • v8.10.0.