Using python request library to dynamically get HTTP Error String
<p>I've been asked to swap over from urllib2 to the requests library because the library is simpler to use and doesn't cause exceptions.</p>

<p>I can get the HTTP error code with response.status_code but I don't see a way to get the error message for that code. Normally, I wouldn't care, but I'm testing an API and the string is just as important.</p>

<p>Does anybody know of a simple way to get that string? I'm expecting 2 pieces something like:</p>

<p>'400':'Bad Request'</p>

<p>This is NOT a DUPLICATE</p>

<p>Some of the codes being returned have unique strings being sent by the application that I am testing. These strings cannot be looked up using this method: requests.status_codes._codes[error][0] Since the string is dynamically coming from the back end server. I was able to get this information using urllib using this method: </p>

<p><code>import urllib2
 ...
 opener = urllib2.build_opener(urllib2.HTTPSHandler(context=ctx))
 except urllib2.HTTPError as err:
   try: error_message = err.read()
 ...</code></p>

<p>The question now is... is there a method of getting the dynamic http error string?  Thanks so much for being patient. The previous issue was closed so quickly I never got a chance to look at the answer, test it and re-ask by cleaning up the description.</p>

<pre><code>response = requests.get(url)
error_message = response.reason
</code></pre>

python httprequest urllib2