In an earlier article, we learned about 7 different ways to making HTTP requests in Node.js using various popular libraries such as Axios, Needle, and more. No doubt these libraries are simple and hide the underlying complexity of dealing with HTTP requests in native Node.js. But it also requires to add external dependencies.

In this short article, you will learn about Node.js native HTTPS module to make HTTP requests without any external dependency.

Since it is a native module, no installation is required. You can access in your code like below:

const https = require('https');

GET Request

Here is a very simple example that uses the HTTPS module's https.get() method to send a GET request:

const https = require('https');

https.get('https://reqres.in/api/users', (res) => {
    let data = '';

    // called when a data chunk is received.
    res.on('data', (chunk) => {
        data += chunk;
    });

    // called when the complete response is received.
    res.on('end', () => {
        console.log(JSON.parse(data));
    });

}).on("error", (err) => {
    console.log("Error: ", err.message);
});

Unlike other popular HTTP clients which collect the response and return it as a string or a JSON object, here you need to concatenate the incoming data stream to use it later. Another notable exception is the HTTPS module does not support promises which makes sense as it is a low-level module and is not very user-friendly.

POST Request

To make a POST request, we have to use the generic https.request() method. There is no shorthand https.post() method available.

The https.request() method accepts two parameters:

  • options — It can be an object literal, a string or a URL object.
  • callback — The callback function to capture and process the response.

Let us make a POST request:

const https = require('https');

const data = JSON.stringify({
    name: 'John Doe',
    job: 'DevOps Specialist'
});

const options = {
    protoco: 'https',
    hostname: 'reqres.in',
    port: 443,
    path: '/api/users',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': data.length
    }
};


const req = https.request(options, (res) => {
    let data = '';

    res.on('data', (chunk) => {
        data += chunk;
    });

    res.on('end', () => {
        console.log(JSON.parse(data));
    });

}).on("error", (err) => {
    console.log("Error: ", err.message);
});

req.write(data);
req.end();

The protocol and port attributes in the options object are optional.

PUT and DELETE Requests

The PUT and DELETE requests format are similar to a POST request. Just change the options.method value to PUT or DELETE.

Here is an example of DELETE request:

const https = require('https');

const options = {
    hostname: 'reqres.in',
    path: '/api/users/2',
    method: 'DELETE'
};


const req = https.request(options, (res) => {

    // log the status
    console.log('Status Code:', res.statusCode);

}).on("error", (err) => {
    console.log("Error: ", err.message);
});

req.end();

✌️ 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.