In Node.js (using Express.js), when I call http.request
like such:
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 the query
part of options is ignored. Documentation says the query string must be constructed manually, and passed inside path
: something like path: '/?name=John%20Doe&age=50'
.
What is the best way to achieve that? query
is a simple hash of string->{string, number}.
What you're looking for is the querystring library http://nodejs.org/api/querystring.html
And also, you might 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='