1

I have this code

try:
    response = requests.post(url, data=json.dumps(payload))
except (ConnectionError, HTTPError):
    msg = "Connection problem"
    raise Exception(msg)

Now i want the following

if status_code == 401
   login() and then try request again
if status_code == 400
   then send respose as normal
if status_code == 500
   Then server problem , try the request again and if not successful raise   EXception

Now these are status codes , i donn't know how can i mix status codes with exceptions. I also don't know what codes will be covered under HttpError

2 Answers 2

3

requests has a call called raise_for_status available in your request object which will raise an HTTPError exception if any code is returned in the 400 to 500 range inclusive.

Documentation for raise_for_status is here

So, what you can do, is after you make your call:

response = requests.post(url, data=json.dumps(payload))

You make a call for raise_for_status as

response.raise_for_status()

Now, you are already catching this exception, which is great, so all you have to do is check to see which status code you have in your error. This is available to you in two ways. You can get it from your exception object, or from the request object. Here is the example for this:

from requests import get
from requests.exceptions import HTTPError

try:
    r = get('http://google.com/asdf')
    r.raise_for_status()
except HTTPError as e:
    # Get your code from the exception object like this
    print(e.response.status_code)
    # Or you can get the code which will be available from r.status_code
    print(r.status_code)

So, with the above in mind, you can now use the status codes in your conditional statements

Sign up to request clarification or add additional context in comments.

7 Comments

I could not get one thing, if i am using except then why i would need to use raise_for_status, also for connectionError where do i put that statement
Try statements may have more than one except block, you probably don't want to be handling a connection error the same way as you do an HTTP error.
@JohnKaff requests will not raise an HTTPError on its own. You have to check for the status_code and act accordingly in your code.
One thing more , in case of your above code. if use except block and do my stuff. Will 500 exception bubble up or not. I have decrator for retying on code 500. will that work with your above code as example
I don't know how your decorator is implemented. I suggest testing to find out.
|
1

https://docs.python.org/2/library/urllib2.html#urllib2.URLError

code
An HTTP status code as defined in RFC 2616. This numeric value 
corresponds to a value found in the dictionary of codes as found in 
BaseHTTPServer.BaseHTTPRequestHandler.responses.

You can get the error code from an HTTPError from its code member, like so

try:
    # ...
except HTTPError as ex:
    status_code = ex.code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.