Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

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