quickstart.rst
This file
Path
docs/user/quickstart.rst
138 lines · 3.2 KiB
Last changed by
Typos, nits and some 80w fixesjbergstroembefore adoption75027f9
History of this fileTake it
The walk stopped after 200 commits: lines older than 2e4724c point at it.
massive documentation updateKenneth Reitzbefore adoptiond50742f
line 1why, one step back1.. _quickstart:historyKenneth Reitzbefore adoption2e4724c
lines 2–5why, one step back23Quickstart4==========5models..Kenneth Reitzbefore adoption2cf0dad
line 6why, one step back6.. module:: requests.modelshistoryKenneth Reitzbefore adoption2e4724c
line 7why, one step back7Typos, nits and some 80w fixesjbergstroembefore adoption75027f9
lines 8–10why, one step back8Eager to get started? This page gives a good introduction in how to get started9with Requests. This assumes you already have Requests installed. If you do not,10head over to the :ref:`Installation <install>` section.historyKenneth Reitzbefore adoption2e4724c
line 11why, one step back11massive documentation updateKenneth Reitzbefore adoptiond50742f
line 12why, one step back12First, make sure that:historyKenneth Reitzbefore adoption2e4724c
line 13why, one step back13whoopsKenneth Reitzbefore adoption49e5df1
lines 14–15why, one step back14* Requests is :ref:`installed <install>`15* Requests is :ref:`up-to-date <updates>`historyKenneth Reitzbefore adoption2e4724c
lines 16–17why, one step back1617massive documentation updateKenneth Reitzbefore adoptiond50742f
line 18why, one step back18Lets gets started with some simple use cases and examples.historyKenneth Reitzbefore adoption2e4724c
lines 19–20why, one step back1920massive documentation updateKenneth Reitzbefore adoptiond50742f
line 21why, one step back21Make a GET RequesthistoryKenneth Reitzbefore adoption2e4724c
lines 22–23why, one step back22------------------23massive documentation updateKenneth Reitzbefore adoptiond50742f
line 24why, one step back24Making a standard request with Requests is very simple.historyKenneth Reitzbefore adoption2e4724c
line 25why, one step back25massive documentation updateKenneth Reitzbefore adoptiond50742f
line 26why, one step back26Let's get GitHub's public timeline ::historyKenneth Reitzbefore adoption2e4724c
line 27why, one step back27massive documentation updateKenneth Reitzbefore adoptiond50742f
line 28why, one step back28 r = requests.get('https://github.com/timeline.json')historyKenneth Reitzbefore adoption2e4724c
line 29why, one step back29Typos, nits and some 80w fixesjbergstroembefore adoption75027f9
lines 30–31why, one step back30Now, we have a :class:`Response` object called ``r``. We can get all the31information we need from this.historyKenneth Reitzbefore adoption2e4724c
lines 32–33why, one step back3233massive documentation updateKenneth Reitzbefore adoptiond50742f
lines 34–35why, one step back34Response Content35----------------historyKenneth Reitzbefore adoption2e4724c
line 36why, one step back36massive documentation updateKenneth Reitzbefore adoptiond50742f
line 37why, one step back37We can read the content of the server's response::historyKenneth Reitzbefore adoption2e4724c
line 38why, one step back38massive documentation updateKenneth Reitzbefore adoptiond50742f
lines 39–40why, one step back39 >>> r.content40 '[{"repository":{"open_issues":0,"url":"https://github.com/...historyKenneth Reitzbefore adoption2e4724c
lines 41–42why, one step back4142massive documentation updateKenneth Reitzbefore adoptiond50742f
lines 43–44why, one step back43Response Status Codes44---------------------historyKenneth Reitzbefore adoption2e4724c
line 45why, one step back45massive documentation updateKenneth Reitzbefore adoptiond50742f
line 46why, one step back46We can check the response status code::historyKenneth Reitzbefore adoption2e4724c
line 47why, one step back47massive documentation updateKenneth Reitzbefore adoptiond50742f
lines 48–49why, one step back48 >>> r.status_code49 200historyKenneth Reitzbefore adoption2e4724c
line 50why, one step back50massive documentation updateKenneth Reitzbefore adoptiond50742f
lines 51–55why, one step back51Requests also comes with a built-in status code lookup object for easy52reference::5354 >>> r.status_code == requests.codes.ok55 Trueraise_for_status quick startKenneth Reitzbefore adoptionf137db6
lines 56–69why, one step back5657If we made a bad request, we can raise it with58:class:`Response.raise_for_status()`::5960 >>> _r = requests.get('http://httpbin.org/status/404')61 >>> _r.status_code62 4046364 >>> _r.raise_for_status()65 Traceback (most recent call last):66 File "requests/models.py", line 394, in raise_for_status67 raise self.error68 urllib2.HTTPError: HTTP Error 404: NOT FOUND69typoKenneth Reitzbefore adoption5f603ae
line 70why, one step back70But, since our ``status_code`` was ``200``, when we call it::raise_for_status quick startKenneth Reitzbefore adoptionf137db6
lines 71–75why, one step back7172 >>> r.raise_for_status()73 None7475All is well.Response Headers quickstartKenneth Reitzbefore adoption2357b13
lines 76–95why, one step back767778Response Headers79----------------8081We can view the server's response headers with a simple Python dictionary82interface::8384 >>> r.headers85 {86 'status': '200 OK',87 'content-encoding': 'gzip',88 'transfer-encoding': 'chunked',89 'connection': 'close',90 'server': 'nginx/1.0.4',91 'x-runtime': '148ms',92 'etag': '"e1ca502697e5c9317743dc078f67693f"',93 'content-type': 'application/json; charset=utf-8'94 }95Typos, nits and some 80w fixesjbergstroembefore adoption75027f9
lines 96–97why, one step back96The dictionary is special, though: it's made just for HTTP headers. According to97`RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTPResponse Headers quickstartKenneth Reitzbefore adoption2357b13
lines 98–111why, one step back98Headers are case-insensitive.99100So, we can access the headers using any capitalization we want::101102 >>> r.headers['Content-Type']103 'application/json; charset=utf-8'104105 >>> r.headers.get('content-type')106 'application/json; charset=utf-8'107108If a header doesn't exist in the Response, its value defaults to ``None``::109110 >>> r.headers['X-Random']111 Nonecookies docsKenneth Reitzbefore adoption3992ed6
lines 112–124why, one step back112113114Cookies115-------116117If a response contains some Cookies, you can get quick access to them::118119 >>> url = 'http://httpbin.org/cookies/set/requests-is/awesome'120 >>> r = requests.get(url)121122 >>> print r.cookies123 {'requests-is': 'awesome'}124Typos, nits and some 80w fixesjbergstroembefore adoption75027f9
line 125why, one step back125The underlying CookieJar is also available for more advanced handling::cookies docsKenneth Reitzbefore adoption3992ed6
lines 126–138why, one step back126127 >>> r.request.cookiejar128 <cookielib.CookieJar>129130To send your own cookies to the server, you can use the ``cookies``131parameter::132133 >>> url = 'http://httpbin.org/cookies'134 >>> cookies = dict(cookies_are='working')135136 >>> r = requests.get(url, cookies=cookies)137 >>> r.content138 '{"cookies": {"cookies_are": "working"}}'