Event hooks and raise_for_status #2224
Answered
by
daviddavis
daviddavis
asked this question in
Potential Issue
|
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? |
Answered by
daviddavis
May 11, 2022
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 |
0 replies
Answer selected by
daviddavis
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.