Categories
JavaScript Answers

How to make Node.js http ‘get’ request with query string parameters?

Spread the love

To make Node.js http ‘get’ request with query string parameters, we set the qs property.

For instance, we write

const request = require("request");
const propertiesObject = { field1: "test1", field2: "test2" };

request({ url, qs: propertiesObject }, (err, response, body) => {
  if (err) {
    console.log(err);
    return;
  }
  console.log(response.statusCode);
});

to call request to make a request to url and we set the quert string by setting qs to an object with the keys and values for the query string.

Then we get the response from the response parameter and the response body from the body parameter.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *