Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 2e4724c point at it.

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 1why, one step back
1.. _quickstart:

historyKenneth Reitzbefore adoption2e4724c

lines 2–5why, one step back
2
3Quickstart4==========5

models..Kenneth Reitzbefore adoption2cf0dad

line 6why, one step back
6.. module:: requests.models

historyKenneth Reitzbefore adoption2e4724c

line 7why, one step back
7

Typos, nits and some 80w fixesjbergstroembefore adoption75027f9

lines 8–10why, one step back
8Eager to get started? This page gives a good introduction in how to get started
9with 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 back
11

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 12why, one step back
12First, make sure that:

historyKenneth Reitzbefore adoption2e4724c

line 13why, one step back
13

whoopsKenneth Reitzbefore adoption49e5df1

lines 14–15why, one step back
14* Requests is :ref:`installed <install>`
15* Requests is :ref:`up-to-date <updates>`

historyKenneth Reitzbefore adoption2e4724c

lines 16–17why, one step back
16
17

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 18why, one step back
18Lets gets started with some simple use cases and examples.

historyKenneth Reitzbefore adoption2e4724c

lines 19–20why, one step back
19
20

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 21why, one step back
21Make a GET Request

historyKenneth Reitzbefore adoption2e4724c

lines 22–23why, one step back
22------------------
23

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 24why, one step back
24Making a standard request with Requests is very simple.

historyKenneth Reitzbefore adoption2e4724c

line 25why, one step back
25

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 26why, one step back
26Let's get GitHub's public timeline ::

historyKenneth Reitzbefore adoption2e4724c

line 27why, one step back
27

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 28why, one step back
28 r = requests.get('https://github.com/timeline.json')

historyKenneth Reitzbefore adoption2e4724c

line 29why, one step back
29

Typos, nits and some 80w fixesjbergstroembefore adoption75027f9

lines 30–31why, one step back
30Now, we have a :class:`Response` object called ``r``. We can get all the
31information we need from this.

historyKenneth Reitzbefore adoption2e4724c

lines 32–33why, one step back
32
33

massive documentation updateKenneth Reitzbefore adoptiond50742f

lines 34–35why, one step back
34Response Content
35----------------

historyKenneth Reitzbefore adoption2e4724c

line 36why, one step back
36

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 37why, one step back
37We can read the content of the server's response::

historyKenneth Reitzbefore adoption2e4724c

line 38why, one step back
38

massive documentation updateKenneth Reitzbefore adoptiond50742f

lines 39–40why, one step back
39 >>> r.content
40 '[{"repository":{"open_issues":0,"url":"https://github.com/...

historyKenneth Reitzbefore adoption2e4724c

lines 41–42why, one step back
41
42

massive documentation updateKenneth Reitzbefore adoptiond50742f

lines 43–44why, one step back
43Response Status Codes
44---------------------

historyKenneth Reitzbefore adoption2e4724c

line 45why, one step back
45

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 46why, one step back
46We can check the response status code::

historyKenneth Reitzbefore adoption2e4724c

line 47why, one step back
47

massive documentation updateKenneth Reitzbefore adoptiond50742f

lines 48–49why, one step back
48 >>> r.status_code
49 200

historyKenneth Reitzbefore adoption2e4724c

line 50why, one step back
50

massive documentation updateKenneth Reitzbefore adoptiond50742f

lines 51–55why, one step back
51Requests also comes with a built-in status code lookup object for easy
52reference::5354 >>> r.status_code == requests.codes.ok55 True

raise_for_status quick startKenneth Reitzbefore adoptionf137db6

lines 56–69why, one step back
56
57If 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 FOUND69

typoKenneth Reitzbefore adoption5f603ae

line 70why, one step back
70But, since our ``status_code`` was ``200``, when we call it::

raise_for_status quick startKenneth Reitzbefore adoptionf137db6

lines 71–75why, one step back
71
72 >>> r.raise_for_status()73 None7475All is well.

Response Headers quickstartKenneth Reitzbefore adoption2357b13

lines 76–95why, one step back
76
7778Response 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 }95

Typos, nits and some 80w fixesjbergstroembefore adoption75027f9

lines 96–97why, one step back
96The dictionary is special, though: it's made just for HTTP headers. According to
97`RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTP

Response Headers quickstartKenneth Reitzbefore adoption2357b13

lines 98–111why, one step back
98Headers 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 None

cookies docsKenneth Reitzbefore adoption3992ed6

lines 112–124why, one step back
112
113114Cookies115-------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'}124

Typos, nits and some 80w fixesjbergstroembefore adoption75027f9

line 125why, one step back
125The underlying CookieJar is also available for more advanced handling::

cookies docsKenneth Reitzbefore adoption3992ed6

lines 126–138why, one step back
126
127 >>> 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"}}'