Skip to content

http.get method() ignores query params when passing an URL as first argument #22162

Description

@gabriel-araujjo
  • Version: v8.10.0
  • Platform: Linux 4.15.0-29-generic Ubuntu SMP Tue Jul 17 15:39:52 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: http

When passing an URL with query params as the first argument to http.get() method, the query is ignored.

const URL = require('url');
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 = URL.parse('http://httpbin.org/get');
url.query = {foo: 'bar'}

http.get(url, parseJsonResp(function(err, obj) {
  assert(obj.args.foo === 'bar'); // It fails here
}));

Activity

  1. 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
  2. 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
  3. devsnek commented on Aug 6, 2018

    @devsnek
    Member

    http.get doesn't expect a whatwg url object, it expects an options argument similar to the output of url.parse()

  4. added
    questionIssues asking questions about Node.js.
    httpIssues and PRs related to the http subsystem.
    urlIssues and PRs related to the legacy built-in url module.
    whatwg-urlIssues and PRs related to the WHATWG URL implementation.
    on Aug 6, 2018
  5. gabriel-araujjo commented on Aug 6, 2018

    @gabriel-araujjo
    Author

    The documentation says that the first argument can be an Object, a string or an URL.

  6. rubys commented on Aug 6, 2018

    @rubys
    Member

    It is a very subtle thing... the documentation says URL, and what you passed was a Url. 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.parse to new URL
    • Change url.query = to url.searchParams.set()
  7. gabriel-araujjo commented on Aug 7, 2018

    @gabriel-araujjo
    Author

    OK. Thanks! 👍. Shouldn't this be explicit on documentation?

  8. rubys commented on Aug 7, 2018

    @rubys
    Member

    @gabriel-araujjo just to be clear, this issue can be closed now? Ah, missed your edit. Suggestions on how to make it more clear?

  9. gabriel-araujjo commented on Aug 7, 2018

    @gabriel-araujjo
    Author

    I guess an example with that distinction is enough.

  10. added
    docIssues and PRs related to Node.js documentation.
    on Aug 12, 2018
  11. added a commit that references this issue on Jun 6, 2020
  12. jasnell commented on Jun 25, 2020

    @jasnell
    Member

    The http URL has since been updated to support URL objects and properly handles the query string now so this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.httpIssues and PRs related to the http subsystem.questionIssues asking questions about Node.js.urlIssues and PRs related to the legacy built-in url module.whatwg-urlIssues and PRs related to the WHATWG URL implementation.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions