Rewriting the url of a httpx.Request #3537
Unanswered
maxa-david-francoeur
asked this question in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have an implementation that writes code like:
the
self.http_client
is ahttpx.AsyncHttpClient
with a Custom authentication scheme on it (it adds a token of the authorization header) and aevent_hooks
for request that sets the absolute url for the request.The hook is set at construction time, via:
event_hooks={"request": [set_github_url]},
and initially I implemented
set_github_url
as follow:But this does not work. Because the url is now absolute, but the headers on the request don't have the Host header set. When you build a
httpx.Request
with an absolute url, aself._prepare(headers)
call will do that for you, but if you set theurl
directly it does not.so instead my implementation is as follow:
That works, but I find it ugly. Could we have a
set_url
or ajoin_url
function the request object that sets the right header for us? Maybe event better acopy_with(url=...)
kind of method that return a new copy of the request and leave the older one untouched.Does that make sense?
Beta Was this translation helpful? Give feedback.
All reactions