Skip to content

Event hooks and raise_for_status #2224

Discussion options

You must be logged in to vote

It looks like if I call read() in raise_on_4xx_5xx then things work as expected (no exception):

import httpx

def raise_on_4xx_5xx(response):
    response.read()
    response.raise_for_status()

client = httpx.Client(event_hooks={'response': [raise_on_4xx_5xx]})

try:
    resp = client.get("http://httpbin.org/status/400")
except httpx.HTTPStatusError as e:
    print(f"Body: {e.response.text}"). # httpx.ResponseNotRead

I'm guessing that raise_for_status closes the response so it's necessary to call read before raise_for_status.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by daviddavis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant