0

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.

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.

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

'400':'Bad Request'
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Keith
  • 4,129
  • 3
  • 13
  • 25
  • Well not very elegant but I wrote this to make it easier to deal with: `def GetHTTPErrorStr( self, error ): errStr = '' try: errStr = requests.status_codes._codes[error][0] except: errStr = 'Unknown HTTP Error in GetHTTPErrorStr' return errStr ` – Keith Oct 19 '16 at 01:26
  • I was trying to add code chunks but it isn't working ` def GetHTTPErrorStr( self, error ): errStr = '' try: errStr = requests.status_codes._codes[error][0] except: errStr = 'Unknown HTTP Error in GetHTTPErrorStr' return errStr` – Keith Oct 19 '16 at 01:31
  • 1
    Actually... this unfortunately isn't a good enough answer. 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: except urllib2.HTTPError as err: try: error_message = err.read() – Keith Oct 19 '16 at 02:28

0 Answers0