Event hooks and raise_for_status #2224
-
|
I'm using an example from the docs to I would expect these two examples to work: import httpx
try:
resp = httpx.get("http://httpbin.org/status/400")
resp.raise_for_status()
except httpx.HTTPStatusError as e:
print(f"Body: {e.response.text}")import httpx
def raise_on_4xx_5xx(response):
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.ResponseNotReadBut the second one doesn't work. It raises a Anyone know how I can get this working so I can read the response content? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
It looks like if I call 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.ResponseNotReadI'm guessing that |
Beta Was this translation helpful? Give feedback.
It looks like if I call
read()inraise_on_4xx_5xxthen things work as expected (no exception):I'm guessing that
raise_for_statuscloses the response so it's necessary to callreadbeforeraise_for_status.