From f137db6803d4feadf0616dfb74059df2d6586dc1 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 14 Aug 2011 22:20:03 -0400 Subject: [PATCH] raise_for_status quick start --- docs/user/quickstart.rst | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index 02d66f6..06d5f28 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -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 \ No newline at end of file + 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. \ No newline at end of file -- 2.7.4