Repository navigation
http.get method() ignores query params when passing an URL as first argument #22162
Copy link
Copy link
Closed
Labels
docIssues and PRs related to Node.js documentation.Issues and PRs related to Node.js documentation.httpIssues and PRs related to the http subsystem.Issues and PRs related to the http subsystem.questionIssues asking questions about Node.js.Issues asking questions about Node.js.urlIssues and PRs related to the legacy built-in url module.Issues and PRs related to the legacy built-in url module.whatwg-urlIssues and PRs related to the WHATWG URL implementation.Issues and PRs related to the WHATWG URL implementation.
Description
Activity
- changed the title
[-]HTTP get ignores query params when passing address through url[/-][+]http.get method ignores query params when passing an URL as first argument[/+]on Aug 6, 2018 - changed the title
[-]http.get method ignores query params when passing an URL as first argument[/-][+]http.get method() ignores query params when passing an URL as first argument[/+]on Aug 6, 2018 http.get doesn't expect a whatwg url object, it expects an options argument similar to the output of
url.parse()- addedquestionIssues asking questions about Node.js.Issues asking questions about Node.js.httpIssues and PRs related to the http subsystem.Issues and PRs related to the http subsystem.urlIssues and PRs related to the legacy built-in url module.Issues and PRs related to the legacy built-in url module.whatwg-urlIssues and PRs related to the WHATWG URL implementation.Issues and PRs related to the WHATWG URL implementation.
on Aug 6, 2018 The documentation says that the first argument can be an
Object, astringor anURL.It is a very subtle thing... the documentation says
URL, and what you passed was aUrl. Click on the links to see the difference.Try the following:
const http = require('http'); const assert = require('assert'); function parseJsonResp(next) { return function(resp) { var raw = ''; resp.on('data', function(chunk) { raw += chunk; }); resp.on('end', function() { next(null, JSON.parse(raw)); }); } }; const url = new URL('http://httpbin.org/get'); url.searchParams.set('foo', 'bar') http.get(url, parseJsonResp(function(err, obj) { assert(obj.args.foo === 'bar'); // It fails here }));
Key changes:
- Remove
require('url') - Change
URL.parsetonew URL - Change
url.query =tourl.searchParams.set()
- Remove
OK. Thanks! 👍. Shouldn't this be explicit on documentation?
Reacted by Ruben Bridgewater@gabriel-araujjo just to be clear, this issue can be closed now? Ah, missed your edit. Suggestions on how to make it more clear?
I guess an example with that distinction is enough.
Reacted by Ruben Bridgewater- addeddocIssues and PRs related to Node.js documentation.Issues and PRs related to Node.js documentation.
on Aug 12, 2018 - added a commit that references this issue
on Jun 6, 2020 The http URL has since been updated to support URL objects and properly handles the query string now so this can be closed.
Metadata
Metadata
Assignees
Labels
docIssues and PRs related to Node.js documentation.Issues and PRs related to Node.js documentation.httpIssues and PRs related to the http subsystem.Issues and PRs related to the http subsystem.questionIssues asking questions about Node.js.Issues asking questions about Node.js.urlIssues and PRs related to the legacy built-in url module.Issues and PRs related to the legacy built-in url module.whatwg-urlIssues and PRs related to the WHATWG URL implementation.Issues and PRs related to the WHATWG URL implementation.
When passing an
URLwith query params as the first argument tohttp.get()method, the query is ignored.