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"?
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.
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.
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
You can check actual codes in the source. ok
means 200 only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With