Search code examples
pythonaiohttp

Where i can write about response field - .ok?


Any response have a field .ok, for example in code:

async with ClientSession() as session:
    async with session.get(url=url) as response:
        if response.ok:
             ...

My question is reponse.status == 200 equal response.ok?

And for example if status=204, then reponse.ok is False?


Solution

  • From the documentation:

    property ok

    Returns True if status_code is less than 400, False if not.

    This attribute checks if the status code of the response is between 400 and 600 to see if there was a client error or a server error. If the status code is between 200 and 400, this will return True. This is not a check to see if the response code is 200 OK.

    So with response.status = 204 it will still be True