0

I see that the request package is the standard for making HTTP API requests with NodeJS. I need to use it to send some requests but in the docs and all the examples I find, I don't see how to pass GET variables. I only see how to pass POST params. Here's my code:

request.get("https://api.example.com", function (err, res, body) {
        if (!err) {
            var resultsObj = JSON.parse(body);
            //Just an example of how to access properties:
            console.log(resultsObj.MRData);
        }
    });

Where to set the GET? I don't like doing it in the URL.

2
  • GET parameters are part of the URL, so why would you want to put them anywhere else? Commented Oct 4, 2017 at 8:18
  • GET variables are usually standard to be encoded in the url itself, you're not supposed to pass 'body' data in the same way as a POST request. Send them as a query string Commented Oct 4, 2017 at 8:19

1 Answer 1

1

You're looking for the qs property. From the docs:

qs - object containing querystring values to be appended to the uri

request({
  qs: {
   foo: 'bar',
  },
  uri: 'http://foo.bar/'
}, callback)
Sign up to request clarification or add additional context in comments.

3 Comments

Ahh. Not a very common name for it. Thanks.
@xendi — It is a very common name for it, and also the one used in the various web standards.
I know it's technically around in docs but usually you just read people say "Get request/variables." It's been quite a while since I read querystring. Maybe it's just me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.