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.ResponseNotRead But 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.
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.ResponseNotRead I'm guessing that |
Beta Was this translation helpful? Give feedback.
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_5xx
then things work as expected (no exception):I'm guessing that
raise_for_status
closes the response so it's necessary to callread
beforeraise_for_status
.