Repository navigation
http: Using URL with http.request (feature suggestion) #20795
Description
Activity
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?Reacted by Benjamin Gruenbaum- addedhttpIssues and PRs related to the http subsystem.Issues and PRs related to the http subsystem.feature requestIssues requesting new Node.js features.Issues requesting new Node.js features.whatwg-urlIssues and PRs related to the WHATWG URL implementation.Issues and PRs related to the WHATWG URL implementation.
on May 17, 2018 Seems like a reasonable feature to me but what takes precedence when both
.pathand.urlare present? Why?Alternative: overload
http.request()with a 3-arghttp.request(url[, options, callback]).Reacted by GPI'm 👍 for a 3-arg signature.
Reacted by Horaddrim@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? :)).
Reacted by Rich Trott and HoraddrimFun fact: in
fs.createReadStream(path[, options]), whenoptions.fdis specified,pathgets ignored.Reacted by Benjamin Gruenbaum and Ron KorvingReacted by Benjamin Gruenbaum, Horaddrim and GPSlightly related whatwg/fetch#452
@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.toStringwill do the trick for URL objects. I think that behavior (strings and URL objects) makes sense for us too.Reacted by Horaddrim- added a commit that references this issue
on Jul 27, 2026
Using
URLobjects is clearly the way to go when it comes to dealing with URLs, andhttp.request()can take aURLobject 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 calledurlthat 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.