Open In App

Node.js https.request() Function

Node.js provides two core modules for making http requests. The http module can be used to make http requests and the https module can be used to make https requests. One great feature of the request is that it provides a single module that can make both http and https requests.

Feature of https module:



  1. It is easy to get started and easy to use.
  2. It is widely used and popular module for making https calls.

Create a folder and add a file for example index.js, To run this file you need to run the following command.

node index.js

Filename: index.js






const https = require('https');
  
// Sample URL
  
const request = https.request(url, (response) => {
    let data = '';
    response.on('data', (chunk) => {
        data = data + chunk.toString();
    });
  
    response.on('end', () => {
        const body = JSON.parse(data);
        console.log(body);
    });
})
  
request.on('error', (error) => {
    console.log('An error', error);
});
  
request.end() 

Steps to run the program:

So this is how you can use the https.request() function to make https request calls.

Article Tags :