Skip to content
Myr

quickstart.rst

This file

Path

docs/user/quickstart.rst

138 lines · 3.2 KiB

Last changed by

Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef

History of this file

Take it

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 2why, one step back
2

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 3–4why, one step back
3Quickstart
4==========

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 5why, one step back
5

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 7why, one step back
7

Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef

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.

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 11why, one step back
11

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 13why, one step back
13

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

lines 16–17why, one step back
16
17

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

lines 19–20why, one step back
19
20

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 21–22why, one step back
21Make a GET Request
22------------------

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 23why, one step back
23

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 25why, one step back
25

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 27why, one step back
27

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 29why, one step back
29

Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef

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.

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

lines 32–33why, one step back
32
33

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 36why, one step back
36

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 38why, one step back
38

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

lines 41–42why, one step back
41
42

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 45why, one step back
45

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 47why, one step back
47

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 50why, one step back
50

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 51–52why, one step back
51Requests also comes with a built-in status code lookup object for easy
52reference::

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 53why, one step back
53

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 54–55why, one step back
54 >>> r.status_code == requests.codes.ok
55 True

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 56why, one step back
56

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 57–58why, one step back
57If we made a bad request, we can raise it with
58:class:`Response.raise_for_status()`::

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 59why, one step back
59

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 60–62why, one step back
60 >>> _r = requests.get('http://httpbin.org/status/404')
61 >>> _r.status_code62 404

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 63why, one step back
63

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 64–68why, one step back
64 >>> _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 FOUND

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 69why, one step back
69

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 71why, one step back
71

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 72–73why, one step back
72 >>> r.raise_for_status()
73 None

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 74why, one step back
74

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

line 75why, one step back
75All is well.

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

lines 76–77why, one step back
76
77

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 78–79why, one step back
78Response Headers
79----------------

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 80why, one step back
80

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 81–82why, one step back
81We can view the server's response headers with a simple Python dictionary
82interface::

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 83why, one step back
83

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 84–94why, one step back
84 >>> r.headers
85 {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 }

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 95why, one step back
95

Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef

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

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

line 98why, one step back
98Headers are case-insensitive.

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 99why, one step back
99

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

line 100why, one step back
100So, we can access the headers using any capitalization we want::

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 101why, one step back
101

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 102–124why, one step back
102 >>> 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 None112113114Cookies115-------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

Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef

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

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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"}}'