Httpx: Add equivalent to requests.response `ok` property

Created on 21 Nov 2019  ·  5Comments  ·  Source: encode/httpx

Hey there, I'm wondering do we have some equivalent of requests.response:ok property in httpx library? If no, Is it planning to be added sometime in the future? thx!

Most helpful comment

response.ok in requests just calls response.raise_for_status in a try/except and returns a boolean based on that. raise_for_status is equivalent between httpx and requests.

Good point. Here's the relevant snippet:
https://github.com/psf/requests/blob/428f7a275914f60a8f1e76a7d69516d617433d30/requests/models.py#L697-L710

class Response(object):
...
    @property
    def ok(self):
        """Returns True if :attr:`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``.
        """
        try:
            self.raise_for_status()
        except HTTPError:
            return False
        return True

is_error and is_success could be good aliases, would need to be documented in compatibility.md

All 5 comments

Not a core dev, so I can't comment on adding that particular feature, but the status codes are accessible via their upper and lower-case names in httpx.codes:

In [1]: import httpx
   ...:
   ...: print(httpx.codes.ok)
200

So you can do:

assert response.status_code == httpx.codes.ok
# or
if response.status_code != httpx.codes.ok:
    ...

And also, the compatible response.raise_for_status() exists, so you can do:

try:
    response = httpx.get(...)
    response.raise_for_status()
except httpx.HTTPError as ex:
    ...

Personally I've not used response.ok before, and only done raise_for_status(), but I can see why that might be a handy check while developing.

So you can do:

assert response.status_code == httpx.codes.ok
# or
if response.status_code != httpx.codes.ok:
    ...

That is not equivalent to what response.ok does in requests.

And also, the compatible response.raise_for_status() exists, so you can do:

try:
  response = httpx.get(...)
  response.raise_for_status()
except httpx.HTTPError as ex:
    ...

response.ok in requests just calls response.raise_for_status in a try/except and returns a boolean based on that. raise_for_status is equivalent between httpx and requests.

Personally I've not used response.ok before, and only done raise_for_status(), but I can see why that might be a handy check while developing.

I've never used response.ok from requests either.

I think it’s a bit confusing, as the naming kinda points towards “True if the status code was 200 OK”

I’d probably be okay with something like an is_error property, mirroring our existing is_redirect property.

I’m not sure about an is_success?

Yeah agree with posts above, @tomchristie I guess both is_error and is_success methods would be useful!

response.ok in requests just calls response.raise_for_status in a try/except and returns a boolean based on that. raise_for_status is equivalent between httpx and requests.

Good point. Here's the relevant snippet:
https://github.com/psf/requests/blob/428f7a275914f60a8f1e76a7d69516d617433d30/requests/models.py#L697-L710

class Response(object):
...
    @property
    def ok(self):
        """Returns True if :attr:`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``.
        """
        try:
            self.raise_for_status()
        except HTTPError:
            return False
        return True

is_error and is_success could be good aliases, would need to be documented in compatibility.md

Was this page helpful?
0 / 5 - 0 ratings