10

Need to capture the response body for a HTTP error in python. Currently using the python request module's raise_for_status(). This method only returns the Status Code and description. Need a way to capture the response body for a detailed error log.

Please suggest alternatives to python requests module if similar required feature is present in some different module. If not then please suggest what changes can be done to existing code to capture the said response body.

Current implementation contains just the following:

resp.raise_for_status()
1
  • What do you mean by "capture"? Commented Feb 5, 2024 at 10:07

4 Answers 4

7

I guess I'll write this up quickly. This is working fine for me:

try:
  r = requests.get('https://www.google.com/404')
  r.raise_for_status()
except requests.exceptions.HTTPError as err:
  print(err.request.url)
  print(err)
  print(err.response.text)
Sign up to request clarification or add additional context in comments.

2 Comments

Instead of the overhead of raising and catching an exception you could just as well check the status code directly if r.status_code >= 400: print(r.text).
Right, this answer is useful if you are trying to find the body from within an exception handler without access to the response object. If you have access to the response object already, you can simply use it.
5

you can do something like below, which returns the content of the response, in unicode.

response.text

or

try:
    r = requests.get('http://www.google.com/nothere')
    r.raise_for_status()
except requests.exceptions.HTTPError as err:
    print(err)
    sys.exit(1) 
# 404 Client Error: Not Found for url: http://www.google.com/nothere

here you'll get the full explanation on how to handle the exception. please check out Correct way to try/except using Python requests module?

2 Comments

To state this more clearly, and maybe what has been missed: the error object has .request and .response attributes that that make it easy to get the transfer details. err.response.content is what is needed.
Why do you terminate the process? What if the HTTPError is caught later on somewhere else in the call chain? Don't do sys.exit(1). Just re-raise the error: raise err.
5

You can log resp.text if resp.status_code >= 400.

Comments

0

There are some tools you may pick up such as Fiddler, Charles, wireshark.

However, those tools can just display the body of the response without including the reason or error stack why the error raises.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.