Refine headers behaviour for Request()
and client.build_request()
#1226
Labels
enhancement
New feature or request
Milestone
Currently
Request()
auto-generates a bunch of headers on__init__
. Having thought about it for a bit I can see some refinements we could make here to:User-Agent
header.Request()
more generally useful, so that it only auto sets mandatory headers.Rather than set the following headings on
Request
, we should instead include them onclient.headers
...User-Agent: python-httpx/1.2.3
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
When passing
Client(headers={...})
or settingclient.headers = {}
we should give precedence to explicitly set headers, but should include these defaults if they're not otherwise set.Or...
If a user wants to actually remove one of these, they can do so by explicitly deleting it, eg...
The recommended way of creating a request instance will continue to be
client.build_request(...)
, but we'll want to highlight that more clearly, as instantiating a plainRequest()
instance will then give you a more basic set of behaviour by only auto-setting mandatory headers, and leaving everything else up to the user...Host
header, if the request URL is an absolute URL.Content-Length
orTransfer-Encoding
header.Both of those cases will defer to any explicitly included header if one exists, and only auto set if those mandatory headers are omitted.
Also
.prepare_headers()
should become a private method.This all ends up being beneficial, because users still get all the regular convenience of
client.build_request()
, but there's also a plainer style for creatingRequest()
instances with complete control over the headers which doesn't make any assumptions about auto-including a set of default headers.(Somewhat refs #1091)
The text was updated successfully, but these errors were encountered: