Search code examples
pythonresthttprequestpython-requestsraspbian

Get status text after failed http-request


Is there any chance to get additional status information from my respose object, if a request fails? At this point, I'm able to get the status code, but in addition, I need the status information text (which describes the error). If you're using jQuery ajax, you could get this text from jqXHR's responseText-attribute.

Is there an equivalent for a python-requests response?

rsp = requests.put(url='{0}recorditems/{1}'
                    .format(Utils.configuration['service']['baseURI']
                            , recorditemOID)
                    , data=body
                    , headers=headers
                    , cert=Utils.configuration['daemon']['certFile']
                    , verify=True)

if rsp.status_code == 200:
    Utils.log('Erfassung {0} synchronisiert'.format(recorditemOID))
    return True
else:
    Utils.log('Status-Code -> {0}'.format(rsp.status_code))

Solution

  • Use the Response.reason attribute:

    r = requests.get('http://www.google.com/')
    print(r.reason)