quickstart.rst
This file
Path
docs/user/quickstart.rst
134 lines · 3.2 KiB
Last changed by
typoKenneth Reitzbefore adoption5f603ae
History of this fileTake it
The walk stopped after 200 commits: lines older than 982fd9d point at it.
massive documentation updateKenneth Reitzbefore adoptiond50742f
line 1why, one step back1.. _quickstart:new config moduleKenneth Reitzbefore adoption982fd9d
lines 2–5why, one step back23Quickstart4==========5models..Kenneth Reitzbefore adoption2cf0dad
line 6why, one step back6.. module:: requests.modelsnew config moduleKenneth Reitzbefore adoption982fd9d
line 7why, one step back7not tablib..Kenneth Reitzbefore adoption414b9dd
line 8why, one step back8Eager to get started? This page gives a good introduction in how to get started with Requests. This assumes you already have Requests installed. If you do not, head over to the :ref:`Installation <install>` section.new config moduleKenneth Reitzbefore adoption982fd9d
line 9why, one step back9massive documentation updateKenneth Reitzbefore adoptiond50742f
line 10why, one step back10First, make sure that:new config moduleKenneth Reitzbefore adoption982fd9d
line 11why, one step back11whoopsKenneth Reitzbefore adoption49e5df1
lines 12–13why, one step back12* Requests is :ref:`installed <install>`13* Requests is :ref:`up-to-date <updates>`new config moduleKenneth Reitzbefore adoption982fd9d
lines 14–15why, one step back1415massive documentation updateKenneth Reitzbefore adoptiond50742f
line 16why, one step back16Lets gets started with some simple use cases and examples.new config moduleKenneth Reitzbefore adoption982fd9d
lines 17–18why, one step back1718massive documentation updateKenneth Reitzbefore adoptiond50742f
line 19why, one step back19Make a GET Requestnew config moduleKenneth Reitzbefore adoption982fd9d
lines 20–21why, one step back20------------------21massive documentation updateKenneth Reitzbefore adoptiond50742f
line 22why, one step back22Making a standard request with Requests is very simple.new config moduleKenneth Reitzbefore adoption982fd9d
line 23why, one step back23massive documentation updateKenneth Reitzbefore adoptiond50742f
line 24why, one step back24Let's get GitHub's public timeline ::new config moduleKenneth Reitzbefore adoption982fd9d
line 25why, one step back25massive documentation updateKenneth Reitzbefore adoptiond50742f
line 26why, one step back26 r = requests.get('https://github.com/timeline.json')new config moduleKenneth Reitzbefore adoption982fd9d
line 27why, one step back27raise_for_status quick startKenneth Reitzbefore adoptionf137db6
line 28why, one step back28Now, we have a :class:`Response` object called ``r``. We can get all the information we need from this.new config moduleKenneth Reitzbefore adoption982fd9d
lines 29–30why, one step back2930massive documentation updateKenneth Reitzbefore adoptiond50742f
lines 31–32why, one step back31Response Content32----------------new config moduleKenneth Reitzbefore adoption982fd9d
line 33why, one step back33massive documentation updateKenneth Reitzbefore adoptiond50742f
line 34why, one step back34We can read the content of the server's response::new config moduleKenneth Reitzbefore adoption982fd9d
line 35why, one step back35massive documentation updateKenneth Reitzbefore adoptiond50742f
lines 36–37why, one step back36 >>> r.content37 '[{"repository":{"open_issues":0,"url":"https://github.com/...new config moduleKenneth Reitzbefore adoption982fd9d
lines 38–39why, one step back3839massive documentation updateKenneth Reitzbefore adoptiond50742f
lines 40–41why, one step back40Response Status Codes41---------------------new config moduleKenneth Reitzbefore adoption982fd9d
line 42why, one step back42massive documentation updateKenneth Reitzbefore adoptiond50742f
line 43why, one step back43We can check the response status code::new config moduleKenneth Reitzbefore adoption982fd9d
line 44why, one step back44massive documentation updateKenneth Reitzbefore adoptiond50742f
lines 45–46why, one step back45 >>> r.status_code46 200new config moduleKenneth Reitzbefore adoption982fd9d
line 47why, one step back47massive documentation updateKenneth Reitzbefore adoptiond50742f
lines 48–52why, one step back48Requests also comes with a built-in status code lookup object for easy49reference::5051 >>> r.status_code == requests.codes.ok52 Trueraise_for_status quick startKenneth Reitzbefore adoptionf137db6
lines 53–66why, one step back5354If 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 FOUND66typoKenneth Reitzbefore adoption5f603ae
line 67why, one step back67But, since our ``status_code`` was ``200``, when we call it::raise_for_status quick startKenneth Reitzbefore adoptionf137db6
lines 68–72why, one step back6869 >>> r.raise_for_status()70 None7172All is well.Response Headers quickstartKenneth Reitzbefore adoption2357b13
lines 73–107why, one step back737475Response Headers76----------------7778We can view the server's response headers with a simple Python dictionary79interface::8081 >>> r.headers82 {83 'status': '200 OK',84 'content-encoding': 'gzip',85 'transfer-encoding': 'chunked',86 'connection': 'close',87 'server': 'nginx/1.0.4',88 'x-runtime': '148ms',89 'etag': '"e1ca502697e5c9317743dc078f67693f"',90 'content-type': 'application/json; charset=utf-8'91 }9293The dictionary is special, though: it's made just for HTTP headers. According to `RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTP94Headers are case-insensitive.9596So, we can access the headers using any capitalization we want::9798 >>> r.headers['Content-Type']99 'application/json; charset=utf-8'100101 >>> r.headers.get('content-type')102 'application/json; charset=utf-8'103104If a header doesn't exist in the Response, its value defaults to ``None``::105106 >>> r.headers['X-Random']107 Nonecookies docsKenneth Reitzbefore adoption3992ed6
lines 108–134why, one step back108109110Cookies111-------112113If a response contains some Cookies, you can get quick access to them::114115 >>> url = 'http://httpbin.org/cookies/set/requests-is/awesome'116 >>> r = requests.get(url)117118 >>> print r.cookies119 {'requests-is': 'awesome'}120121The underlying CookieJar is also available for more advanced handing::122123 >>> r.request.cookiejar124 <cookielib.CookieJar>125126To send your own cookies to the server, you can use the ``cookies``127parameter::128129 >>> url = 'http://httpbin.org/cookies'130 >>> cookies = dict(cookies_are='working')131132 >>> r = requests.get(url, cookies=cookies)133 >>> r.content134 '{"cookies": {"cookies_are": "working"}}'