Skip to content

http: Using URL with http.request (feature suggestion) #20795

Description

@ronkorving
  • Version: v10.1.0
  • Platform: all
  • Subsystem: http

Using URL objects is clearly the way to go when it comes to dealing with URLs, and http.request() can take a URL object as input. This is great. But if I want to also send custom headers, I'm stuck. I would like to suggest an option for the options object called url that can be set to URL object and maybe even a string. I don't really want to make an already complex API more complex, but I'm not sure how else we can make it easier to use URL objects with HTTP requests.

Feedback, suggestions, thoughts from @nodejs/collaborators very welcome.

Activity

  1. Trott commented on May 17, 2018

    @Trott
    Member

    Can you provide some sample code showing the way you would like to be able to write calls to http.request() using this feature? Perhaps with either additional code showing how it has to be done currently?

  2. modified the milestone: on May 17, 2018
  3. added
    httpIssues and PRs related to the http subsystem.
    feature requestIssues requesting new Node.js features.
    whatwg-urlIssues and PRs related to the WHATWG URL implementation.
    on May 17, 2018
  4. bnoordhuis commented on May 17, 2018

    @bnoordhuis
    Member

    Seems like a reasonable feature to me but what takes precedence when both .path and .url are present? Why?

    Alternative: overload http.request() with a 3-arg http.request(url[, options, callback]).

  5. mcollina commented on May 17, 2018

    @mcollina
    SponsorMember

    I'm 👍 for a 3-arg signature.

  6. ronkorving commented on May 17, 2018

    @ronkorving
    ContributorAuthor

    @Trott I was suggesting:

    const options = { url: new URL('...'), headers: { foo: 'bar' } };
    http.request(options, cb);

    @bnoordhuis Good question, and I think your suggestion for 3-arg is a good alternative that makes the precedence very clear (the URL wins, right? :)).

  7. joyeecheung commented on May 17, 2018

    @joyeecheung
    Member

    Fun fact: in fs.createReadStream(path[, options]), when options.fd is specified, path gets ignored.

  8. benjamingr commented on May 17, 2018

    @benjamingr
    Member

    Slightly related whatwg/fetch#452

  9. ronkorving commented on May 18, 2018

    @ronkorving
    ContributorAuthor

    @benjamingr Yeah, I think the takeaway there is that the URL is basically expected to be (or is cast to) a string, and URL.prototype.toString will do the trick for URL objects. I think that behavior (strings and URL objects) makes sense for us too.

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

    feature requestIssues requesting new Node.js features.httpIssues and PRs related to the http subsystem.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