Skip to content
Myr

raise_for_status quick start

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kenneth Reitz · committed by Kenneth Reitz · parent 2cf0dad · Browse files · 1 file+22 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+22 added−4 removed

  • other:+22 added−4 removed

1 file+22 added−4 removed

modified:docs/user/quickstart.rst+22 added-4 removed
23 unmodified lines
24Let's get GitHub's public timeline ::2526 r = requests.get('https://github.com/timeline.json')2728Now, we have a :class:`Response` object. We can get all the information29we need from this.28Now, we have a :class:`Response` object called ``r``. We can get all the information we need from this.293031Response Content32----------------
3 unmodified lines
36 >>> r.content37 '[{"repository":{"open_issues":0,"url":"https://github.com/...38394140Response Status Codes41---------------------4243We can check the response status code::
4 unmodified lines
48Requests also comes with a built-in status code lookup object for easy49reference::5051 >>> r.status_code == requests.codes.ok54 True52 True5354If we made a bad request, we can raise it with55:class:`Response.raise_for_status()`::5657 >>> _r = requests.get('http://httpbin.org/status/404')58 >>> _r.status_code59 4046061 >>> _r.raise_for_status()62 Traceback (most recent call last):63 File "requests/models.py", line 394, in raise_for_status64 raise self.error65 urllib2.HTTPError: HTTP Error 404: NOT FOUND6667But, sice our ``status_code`` was ``200``, when we call it::6869 >>> r.raise_for_status()70 None7172All is well.