5

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}.

1 Answer 1

15

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='
Sign up to request clarification or add additional context in comments.

1 Comment

what about JSON.stringify ?

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.