Open In App

Node.js https.request() Function

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Run index.js file using below command:
    node index.js

    Output of above command

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads