python requests: how to check for "200 OK"

PythonPython Requests

Python Problem Overview


What is the easiest way to check whether the response received from a requests post was "200 OK" or an error has occurred?

I tried doing something like this:

....
resp = requests.post(my_endpoint_var, headers=header_var, data=post_data_var)
print(resp)
if resp == "<Response [200]>":
    print ('OK!')
else:
    print ('Boo!')

The output on the screen is:

Response [200] (including the "<" and ">")
Boo!

So even though I am getting a 200, my check in the if statement is somehow not matching?

Python Solutions


Solution 1 - Python

According to the docs, there's a status_code property on the response-object. So you can do the following:

if resp.status_code == 200:
    print ('OK!')
else:
    print ('Boo!')

EDIT:

As others have pointed out, a simpler check would be

if resp.ok:
    print ('OK!')
else:
    print ('Boo!')

if you want to consider all 2xx response codes and not 200 explicitly. You may also want to check Peter's answer for a more python-like way to do this.

Solution 2 - Python

Just check the response attribute resp.ok. It is True for all 2xx responses, but False for 4xx and 5xx. However, the pythonic way to check for success would be to optionally raise an exception with Response.raise_for_status():

try:
    resp = requests.get(url)
    resp.raise_for_status()
except requests.exceptions.HTTPError as err:
    print(err)

EAFP: It’s Easier to Ask for Forgiveness than Permission: You should just do what you expect to work and if an exception might be thrown from the operation then catch it and deal with that fact.

Solution 3 - Python

Since any 2XX class response is considered successful in HTTP, I would use:

# changed the direction of the less than sign
if 200 <= resp.status_code >= 299:
    print ('OK!') 
else:
    print ('Boo!')

Solution 4 - Python

Much simpler check would be

    if resp.ok :
        print ('OK!')
    else:
        print ('Boo!')

Solution 5 - Python

resp.status_code will return the status code as an integer.

See http://docs.python-requests.org/en/master/

Solution 6 - Python

I'm surprised no one has mentioned this so:

If you want to check for exactly a 200 response:

if resp.status_code == requests.codes.ok:

The status_code of a response contains the HTTP status returned.

requests.codes.ok is exactly 200.


If you want to check if the status code is "ok", and not an error:

if resp.ok:

The ok attribute of a response checks that the status code of the response is less than 400.


Make sure you know what it is you want to check, for example a 201 HTTP Created is a successful response that you would be omitting if you only check for exactly 200.

Solution 7 - Python

try:

if resp.status_code == 200:
    print ('OK!')
else:
    print ('Boo!)

Solution 8 - Python

In easy case:

import requests

response = requests.get(url)
if not response:
    #handle error here
else:
    #handle normal response

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMontyView Question on Stackoverflow
Solution 1 - PythoneolView Answer on Stackoverflow
Solution 2 - PythonPeterView Answer on Stackoverflow
Solution 3 - PythonroubleView Answer on Stackoverflow
Solution 4 - PythonMrHumbleView Answer on Stackoverflow
Solution 5 - PythonorangeInkView Answer on Stackoverflow
Solution 6 - PythongdvalderramaView Answer on Stackoverflow
Solution 7 - PythonCaders117View Answer on Stackoverflow
Solution 8 - PythonqloveshmilyView Answer on Stackoverflow