6

I'm trying to send a GET request from a Node.js app to a Rails server. Currently, I am using the request module like so:

var request = require("request");
var url = 'www.example.com'

function sendRequest(url){
  string = 'http://localhost:3000/my-api-controller?url=' + url;
  request.get(string, function(error, response, body){
    console.log(body);
  });
}

This works. But what I would like is not to build the string for a get request, but to pass parameters of the request as a javascript object (in a jQuery-like fashion). There is one example on the wiki page of the request module that uses exactly this kind of syntax:

request.get('http://some.server.com/', {
  'auth': {
    'user': 'username',
    'pass': 'password',
    'sendImmediately': false
  }
});

However, when I tried to adapt this syntax for my purposes like so:

function sendRequest(url){
  request.get('http://localhost:3000/my-api-controller', {url: url}, function(error, response, body){
    console.log(body);
  });
}

the url parameter did not get sent.

So my question is, am I doing something wrong here or does the request module not support passing parameters of a get request as a javascript object? And if it doesn't, could you suggest a handy Node module that does?

2
  • 1
    Your question doesn't make any sense. You can't have a request body with a GET request, so you can't send any JSON. All you can do is build a query string, but you specifically say (I think anyway) that you don't wnat to do that. What exactly are you trying to do? You know you can use the querystring module to build the query string from an object for you? Commented Apr 14, 2015 at 0:42
  • I must have phrased my question poorly. I am happy to send a built query string as long as the module has instruments for building it for me and I do not have to build it myself by concatenating strings. I was not aware of the querystring module, I'm afraid. Commented Apr 14, 2015 at 1:05

2 Answers 2

7

The "HTTP Authentication" example you point to in the request module does not build a query string, it adds authentication headers based on specific options. There's another part of that page that describes what you want:

request.get({url: "http://localhost:3000/my-api-controller", 
             qs: {url: url}},
            function(error, response, body){
               console.log(body);
            });

Something like that. This, in turn, uses the querystring module to build the query string, as was mentioned in the comments.

Sign up to request clarification or add additional context in comments.

1 Comment

so that's the qs function. I read the documentation but it seems I missed it. Thank you!
6

The object provided to request() or its convenience methods isn't just for data parameters.

To provide { url: url } to be sent in the query-string, you'll want to use the qs option.

request.get('http://localhost:3000/my-api-controller', {
    qs: { url: url }
}, function(error, response, body){
    // ...
});

1 Comment

I see you edited your answer. I tried both the previous syntax — request.get( {url: 'http://localhost:3000/my-api-controller', qs: { url: url }},... — and the updated syntax request.get('http://localhost:3000/my-api-controller', {qs: { url: url }},... — and they both work. Thank you for your explanation, and I wish SO allowed accepting several answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.