Make Response.raise_for_status() return the response object if the response is successful

This allows for chaining method calls in cases where we want to raise for bad codes but use the response otherwise,
e.g. requests.get(URL).raise_for_status().json()['value']
This commit is contained in:
Rotem Yaari
2017-02-11 21:27:50 +02:00
parent b26f4c7f58
commit efcbe93075
4 changed files with 15 additions and 2 deletions
+5 -1
View File
@@ -354,10 +354,14 @@ But, since our ``status_code`` for ``r`` was ``200``, when we call
``raise_for_status()`` we get::
>>> r.raise_for_status()
None
<Response [200]>
All is well.
.. note:: ``raise_for_status`` returns the response object for a successful response. This eases chaining in trivial cases, where we want bad codes to raise an exception, but use the response otherwise:
>>> value = requests.get('http://httpbin.org/ip').raise_for_status().json()['origin']
Response Headers
----------------