Event hook on redirect request not being called twice #1804
-
Hi! Thanks for the brillant library! I love the flexibility it offers when compared to But I think that there could be some issue with the event hook or at least it would be great to clarify this in the documentation. Let's look at this example: import asyncio
import httpx
class AsyncTokenTransport(httpx.AsyncHTTPTransport):
async def handle_async_request(self, method, url, headers, stream, extensions):
print(f"Intercepted {url[1].decode()}")
return await super().handle_async_request(
method, url, headers, stream, extensions
)
async def test_hook(request: httpx.Request) -> None:
print(f"Called for request to {request.url}")
async def main():
async with httpx.AsyncClient(
transport=AsyncTokenTransport(), event_hooks={"request": [test_hook]}
) as client:
# URL is the one of my last school which was handy because it has a redirect configured
r = await client.get("http://cogym.de")
print(r.status_code)
loop = asyncio.get_event_loop()
loop.run_until_complete(main()) The output is
This means, while the transport will be called for both requests, the hook will only be called once. I have a use case where I need to attach an authentication token in the header for every request that needs to be unique for every request. Unfortunately, if I call an API route that redirects me (and attach the token via the event hook), Could you clarify if this is the intended behaviour? Do you want the event hook to only be called once if we follow the redirect and make another request? That definitely was new to me and the requests If this is the intended behaviour, I think it would be great to clarify this in the docs. I am happy to contribute a PR. PS: As my example shows, you can do the thing that I want to do with a custom Transport. But this is not as easy or convenient as the event hook. In the event hook, I can work with the nice top-level |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hola!
So... we've got a choice here, which I think is either...
I had been planning on the first, but looking at it right now, today, I'd expect the second to feel more obvious, and it's certainly less complex to describe and is a simpler, smaller surface-area API. Either way around we ought to document the behaviour. It would also be a good idea for us to review what event_hooks behaviour |
Beta Was this translation helpful? Give feedback.
Hola!
So... we've got a choice here, which I think is either...
I had been planning on the first, but looking at it right now, today, I'd expect the second to feel more obvious, and it's certainly less complex to describe and is a simpler, smaller surface-area API.
E…