Node JS Make Http Get Request with Parameters Example

By Hardik Savani May 1, 2021 Category : Node JS

Hi,

This article will give you example of node js make http get request with parameters. you can understand a concept of node js https request query parameters. you will learn how to make an http get request in node.js. you will learn make http get request nodejs. Let's get started with node js axios http get request example.

i will give you two examples, using axios and request npm package for male get http request with parameters using node js. let's see both example with output as bellow:

Example 1: HTTP GET Request using Axios

Create Node App:

mkdir my-request-app

cd my-request-app

npm init

Install Axios:

npm install axios --save

server.js

const axios = require('axios');

const querystring = require('querystring');

const data = {

page: 2,

};

const parameters = querystring.stringify(data);

axios.get('https://reqres.in/api/users?'+parameters)

.then((res) => {

console.log(`Status: ${res.status}`);

console.log('Body: ', res.data);

}).catch((err) => {

console.error(err);

});

Run App

node server.js

Output

Example 2: HTTP GET Request using Request

Create Node App:

mkdir my-request-app

cd my-request-app

npm init

Install Axios:

npm install request --save

server.js

const request = require('request');

const querystring = require('querystring');

const parameters = {

page: 2,

};

const get_request_args = querystring.stringify(parameters);

const options = {

url: 'https://reqres.in/api/users?'+get_request_args,

json: true

};

request.get(options, (err, res, body) => {

if (err) {

return console.log(err);

}

console.log(`Status: ${res.statusCode}`);

console.log(body);

});

Run App

node server.js

Output

I hope it can help you...

Tags :
Shares