RTF
RTF

Reputation: 6494

How to get HTTP error stream using Python requests library?

How do I access the error stream using the Python requests library? For example, using HttpURLConnection in Java, I would do something like:

InputStream errorStream = conn.getErrorStream();

Is there a function like this with requests?
I'm looking for the error message in the response that was supplied by the source, NOT the error status e.g. Internal Server Error

Upvotes: 1

Views: 1013

Answers (1)

t.m.adam
t.m.adam

Reputation: 15376

requests won't raise an exception if HTTP error occurs, but you can get the error message in the response content. Example:

url = 'https://stackoverflow.com/does_not_exist'
r = requests.get(url)
print(r.status_code, r.reason)
print(r.text)

Upvotes: 1

Related Questions