["python","python-requests"]
How to check for "200 OK"?

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?
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!') As others have pointed out, a simpler check might be to use the ok property: if resp.ok: print ('OK!') else: print ('Boo!') That is, 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 pythonic way to do this.