Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does requests.codes.ok include a 304?

I have a program which uses the requests module to send a get request which (correctly) responds with a 304 "Not Modified". After making the request, I check to make sure response.status_code == requests.codes.ok, but this check fails. Does requests not consider a 304 as "ok"?

like image 382
stett Avatar asked Mar 19 '14 02:03

stett


People also ask

What is response OK in Python?

ok returns True if status_code is less than 400, otherwise False. Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object.

Which series will the HTTP response code belong to in case the server wants to indicate an error with the request?

5xx: Server-Side Error. The 5xx series of status codes is for representing problems on the server side. In most cases, these codes mean the server is not in a state to run the client's request or even see whether it's correct, and that the client should retry its request later.


2 Answers

There is a property called ok in the Response object that returns True if the status code is not a 4xx or a 5xx.

So you could do the following:

if response.ok:     # 304 is included 

The code of this property is pretty simple:

@property def ok(self):     try:         self.raise_for_status()     except HTTPError:         return False     return True 
like image 100
darkheir Avatar answered Sep 20 '22 07:09

darkheir


You can check actual codes in the source. ok means 200 only.

like image 24
Dmitry Shevchenko Avatar answered Sep 17 '22 07:09

Dmitry Shevchenko