Empowering you to understand your world

How To Send A Post Request Using Node.js

By Nicholas Brown.

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).

Node.js POST Request Example

let http = require('http');

let urlparams = {
    host: 'yourserver.com', //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.
        });
    }

    let 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.

Want to store the data from the POST request in a database for later? Here are some helpful tutorials for that:

Mongoose Basics: Storing Data With Node.js And MongoDB

Introduction To Mongoose: Database Read Operations In Node.js

PostgreSQL Tutorials: How To Create A PostgreSQL Database

Send An HTTPS POST Request Using Node.js

The HTTPS POST request example below sends the request over SSL/TLS.

let https = require('https');

let urlparams = {
    host: 'plop.requestcatcher.com', //No need to include 'https://' or 'www.'
    port: 443,
    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.
        });
    }

    let request = https.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.

Tested on Node.js versions:

  • v8.10.0.
  • v20.2.0.

Further Reading

How To Send A GET Request In JavaScript

Leave a Reply

Subscribe to our newsletter
Get notified when new content is published