Http.get and query string in node.js

In Node.js (using Express.js), when I call http.requestlike this:

var options = {
    host: '127.0.0.1',
    port: 80,
    path: '/',
    query: {name: "John Doe", age: 50} // <---- problem here
};
http.request(options, function(response) { ... });

all is well, except that some of the parameters are queryignored. The documentation says that the query string should be built manually and passed inside path: something like path: '/?name=John%20Doe&age=50'.

What is the best way to achieve this? query- a simple hash of the string → {string, number}.

+5
source share
1 answer

What you are looking for is the querystring library http://nodejs.org/api/querystring.html

You might also be interested in this HTTP client request library https://github.com/mikeal/request

var qs = require('querystring');
qs.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
// returns
'foo=bar&baz=qux&baz=quux&corge='
+15
source

All Articles