Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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))
like image 994
Sven Kannenberg Avatar asked Oct 04 '13 07:10

Sven Kannenberg


People also ask

How do I get the HTTP status code of a request response?

The Status-Code element in a server response, is a 3-digit integer where the first digit of the Status-Code defines the class of response and the last two digits do not have any categorization role. There are 5 values for the first digit: S.N. It means the request has been received and the process is continuing.

What number would the status code be if a HTTP GET request has succeeded?

The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default.

Which HTTP status code is usually returned when a resource was found and returned?

The origin server MUST create the resource before returning the 201 status code. If the action cannot be carried out immediately, the server SHOULD respond with 202 (Accepted) response instead.

How do I know if a post request is successful?

If you pick up the result from when you post you can then check the status code: result = Session. post(SubmitURL, data=PostData) if result. status_code == requests.


1 Answers

Use the Response.reason attribute:

r = requests.get('http://www.google.com/')
print(r.reason)
like image 179
Lukasa Avatar answered Oct 23 '22 09:10

Lukasa