Open In App

Node.js Request Module

Last Updated : 19 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The request module in Node is used to make HTTP requests. It allows developers to simply make HTTP requests like GET, POST, PUT, and DELETE in the Node app. It follows redirects by default.

Syntax:

const request = require('request');

request(url, (error, response, body) => {
    if (!error && response.statusCode == 200) {
        console.log(body);  // Handle the response body
    }
});

Note: As of Feb 11th, 2020, the request is fully deprecated.

Features:

  • It is easy to get started and easy to use.
  • It is a widely used and popular module for making HTTP calls.

Steps to Install Request Module

 You can visit the link Install Request module. You can install this package by using this command.

npm install request

After installing the request module you can check your request version in the command prompt using the command.

npm version request

After that, you can 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

The project structure will look like this: 

project structure

Example: This example uses request module to make get request to an API and console log the received data.

JavaScript
// Filename: index.js

const request = require('request')

// Request URL
let url = 'https://jsonplaceholder.typicode.com/todos/1';

request(url, (error, response, body) => {
    // Printing the error if occurred
    if (error) console.log(error)

    // Printing status code
    console.log(response.statusCode);

    // Printing body
    console.log(body);
});

Steps to run the program:

Make sure you have to install express and request modules using the following commands:

npm install request
npm install express

Run the index.js file using the below command:

node index.js

Output of above command

So this is how you can use the request module for making HTTP calls. It is very simple and easy to use.



Next Article

Similar Reads