From 443d07e0d3bcbf5a23314a4ddf95c67bba533ddb Mon Sep 17 00:00:00 2001 From: Hosam Aly Date: Fri, 2 Oct 2015 23:58:48 +0100 Subject: [PATCH 1/4] Fix #2799: Update Quickstart documentation of Response.json() --- docs/user/quickstart.rst | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index f8b78801..49bf67ba 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -145,8 +145,14 @@ There's also a builtin JSON decoder, in case you're dealing with JSON data:: [{u'repository': {u'open_issues': 0, u'url': 'https://github.com/... In case the JSON decoding fails, ``r.json`` raises an exception. For example, if -the response gets a 401 (Unauthorized), attempting ``r.json`` raises ``ValueError: -No JSON object could be decoded`` +the response gets a 204 (No Content), or if the response contains invalid JSON, +attempting ``r.json`` raises ``ValueError: No JSON object could be decoded``. + +It should be noted that the success of the call to ``r.json`` does **not** +indicate the success of the response. Some servers may return a JSON object in a +failed response (e.g. error details with HTTP 500). Such JSON will be decoded +and returned. To check that a request is successful, use ``r.ok == True`` or +``r.raise_for_status()``. Raw Response Content From 086e19d8e175cccb630ee3589affdd2f38075ecd Mon Sep 17 00:00:00 2001 From: Hosam Aly Date: Sat, 3 Oct 2015 16:34:36 +0100 Subject: [PATCH 2/4] Clarify the contract of Response#ok, and avoid exceptions in it --- requests/models.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/requests/models.py b/requests/models.py index 6d6265d0..a822dd15 100644 --- a/requests/models.py +++ b/requests/models.py @@ -616,11 +616,8 @@ class Response(object): @property def ok(self): - try: - self.raise_for_status() - except HTTPError: - return False - return True + """True if the status code does *not* indicate an error, i.e. status code < 400.""" + return self.status_code < 400 @property def is_redirect(self): From a89d5c003a8708e9555968f810311f3e019281c4 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sat, 3 Oct 2015 18:32:28 +0100 Subject: [PATCH 3/4] Revert "Clarify the contract of Response#ok, and avoid exceptions in it" This reverts commit 086e19d8e175cccb630ee3589affdd2f38075ecd. --- requests/models.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index a822dd15..6d6265d0 100644 --- a/requests/models.py +++ b/requests/models.py @@ -616,8 +616,11 @@ class Response(object): @property def ok(self): - """True if the status code does *not* indicate an error, i.e. status code < 400.""" - return self.status_code < 400 + try: + self.raise_for_status() + except HTTPError: + return False + return True @property def is_redirect(self): From 139418475cd6bedd3d383cd3e442698b888c51c2 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sat, 3 Oct 2015 18:33:25 +0100 Subject: [PATCH 4/4] Remove reference to ok. --- docs/user/quickstart.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index 49bf67ba..d08561d2 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -151,8 +151,8 @@ attempting ``r.json`` raises ``ValueError: No JSON object could be decoded``. It should be noted that the success of the call to ``r.json`` does **not** indicate the success of the response. Some servers may return a JSON object in a failed response (e.g. error details with HTTP 500). Such JSON will be decoded -and returned. To check that a request is successful, use ``r.ok == True`` or -``r.raise_for_status()``. +and returned. To check that a request is successful, use +``r.raise_for_status()`` or check ``r.status_code`` is what you expect. Raw Response Content