mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
raise_for_status quick start
This commit is contained in:
@@ -25,8 +25,7 @@ Let's get GitHub's public timeline ::
|
||||
|
||||
r = requests.get('https://github.com/timeline.json')
|
||||
|
||||
Now, we have a :class:`Response` object. We can get all the information
|
||||
we need from this.
|
||||
Now, we have a :class:`Response` object called ``r``. We can get all the information we need from this.
|
||||
|
||||
|
||||
Response Content
|
||||
@@ -38,7 +37,6 @@ We can read the content of the server's response::
|
||||
'[{"repository":{"open_issues":0,"url":"https://github.com/...
|
||||
|
||||
|
||||
|
||||
Response Status Codes
|
||||
---------------------
|
||||
|
||||
@@ -51,4 +49,24 @@ Requests also comes with a built-in status code lookup object for easy
|
||||
reference::
|
||||
|
||||
>>> r.status_code == requests.codes.ok
|
||||
True
|
||||
True
|
||||
|
||||
If we made a bad request, we can raise it with
|
||||
:class:`Response.raise_for_status()`::
|
||||
|
||||
>>> _r = requests.get('http://httpbin.org/status/404')
|
||||
>>> _r.status_code
|
||||
404
|
||||
|
||||
>>> _r.raise_for_status()
|
||||
Traceback (most recent call last):
|
||||
File "requests/models.py", line 394, in raise_for_status
|
||||
raise self.error
|
||||
urllib2.HTTPError: HTTP Error 404: NOT FOUND
|
||||
|
||||
But, sice our ``status_code`` was ``200``, when we call it::
|
||||
|
||||
>>> r.raise_for_status()
|
||||
None
|
||||
|
||||
All is well.
|
||||
Reference in New Issue
Block a user