Skip to content
Myr

quickstart.rst

This file

Path

docs/user/quickstart.rst

254 lines · 6.5 KiB

Last changed by

file uploadsKenneth Reitzbefore adoption40c5480

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

line 41why, one step back
41

big quick start updatesKenneth Reitzbefore adoption60558fd

line 42why, one step back
42Requests does its best to decode content from the server. Most unicode charsets, ``gzip``, and ``deflate`` encodings are all seamlessly decoded.

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 43why, one step back
43

big quick start updatesKenneth Reitzbefore adoption60558fd

line 44why, one step back
44

Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a

line 45why, one step back
45Make a POST Request

big quick start updatesKenneth Reitzbefore adoption60558fd

line 46why, one step back
46-------------------

Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a

line 47why, one step back
47

big quick start updatesKenneth Reitzbefore adoption60558fd

line 48why, one step back
48POST requests are equally simple::

Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a

line 49why, one step back
49

big quick start updatesKenneth Reitzbefore adoption60558fd

line 50why, one step back
50 r = requests.post("http://httpbin.org/post")

Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a

lines 51–52why, one step back
51
52

big quick start updatesKenneth Reitzbefore adoption60558fd

lines 53–111why, one step back
53Typically, you want to send some form-encoded data — much like an HTML form.
54To do this, simply pass a dictionary to the `data` argument. Your dictionary of data will automatically be form-encoded when the request is made::5556 >>> payload = {'key1': 'value1', 'key2': 'value2'}57 >>> r = requests.post("http://httpbin.org/post", data=payload)58 >>> print r.content59 {60 "origin": "179.13.100.4",61 "files": {},62 "form": {63 "key2": "value2",64 "key1": "value1"65 },66 "url": "http://httpbin.org/post",67 "args": {},68 "headers": {69 "Content-Length": "23",70 "Accept-Encoding": "identity, deflate, compress, gzip",71 "Accept": "*/*",72 "User-Agent": "python-requests/0.8.0",73 "Host": "127.0.0.1:7077",74 "Content-Type": "application/x-www-form-urlencoded"75 },76 "data": ""77 }7879There are many times that you want to send data that is not form-encoded. If you pass in a ``string`` instead of a ``dict``, that data will be posted directly.8081For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data::8283 url = 'https://api.github.com/some/endpoint'84 payload = {'some': 'data'}8586 r = requests.post(url, data=json.dumps(payload))878889Custom Headers90--------------9192If you'd like to add HTTP headers to a request, simply pass in a ``dict`` to the93``headers`` parameter.9495For example, we didn't specify our content-type in the previous example::9697 url = 'https://api.github.com/some/endpoint'98 payload = {'some': 'data'}99 headers = {'content-type': 'application/json'}100101 r = requests.post(url, data=json.dumps(payload), headers=headers)102103104POST a Multipart-Encoded File105-----------------------------106107Requests makes it simple to upload Multipart-encoded files::108109 >>> url = 'http://httpbin.org/post'110 >>> files = {'report.xls': open('report.xls', 'rb')}111

file uploadsKenneth Reitzbefore adoption40c5480

line 112why, one step back
112 >>> r = requests.post(url, files=files)

Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a

line 113why, one step back
113 >>> r.content

file uploadsKenneth Reitzbefore adoption40c5480

lines 114–131why, one step back
114 {
115 "origin": "179.13.100.4",116 "files": {117 "hmm": "<censored...binary...data>"118 },119 "form": {},120 "url": "http://httpbin.org/post",121 "args": {},122 "headers": {123 "Content-Length": "3196",124 "Accept-Encoding": "identity, deflate, compress, gzip",125 "Accept": "*/*",126 "User-Agent": "python-requests/0.8.0",127 "Host": "httpbin.org:80",128 "Content-Type": "multipart/form-data; boundary=127.0.0.1.502.21746.1321131593.786.1"129 },130 "data": ""131 }

Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a

line 132why, one step back
132

big quick start updatesKenneth Reitzbefore adoption60558fd

lines 133–136why, one step back
133
134135136

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 137–138why, one step back
137Response Status Codes
138---------------------

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 139why, one step back
139

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 141why, one step back
141

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 142–143why, one step back
142 >>> r.status_code
143 200

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 144why, one step back
144

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 147why, one step back
147

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 150why, one step back
150

big quick start updatesKenneth Reitzbefore adoption60558fd

line 151why, one step back
151If we made a bad request (non-200 response), we can raise it with

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

line 152why, one step back
152:class:`Response.raise_for_status()`::

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 153why, one step back
153

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 154–156why, one step back
154 >>> _r = requests.get('http://httpbin.org/status/404')
155 >>> _r.status_code156 404

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 157why, one step back
157

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 158–162why, one step back
158 >>> _r.raise_for_status()
159 Traceback (most recent call last):160 File "requests/models.py", line 394, in raise_for_status161 raise self.error162 urllib2.HTTPError: HTTP Error 404: NOT FOUND

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 163why, one step back
163

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 165why, one step back
165

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 166–167why, one step back
166 >>> r.raise_for_status()
167 None

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 168why, one step back
168

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

lines 170–171why, one step back
170
171

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 172–173why, one step back
172Response Headers
173----------------

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 174why, one step back
174

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 177why, one step back
177

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 178–188why, one step back
178 >>> r.headers
179 {180 'status': '200 OK',181 'content-encoding': 'gzip',182 'transfer-encoding': 'chunked',183 'connection': 'close',184 'server': 'nginx/1.0.4',185 'x-runtime': '148ms',186 'etag': '"e1ca502697e5c9317743dc078f67693f"',187 'content-type': 'application/json; charset=utf-8'188 }

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 189why, one step back
189

Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef

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

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 193why, one step back
193

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

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

Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea

line 195why, one step back
195

Merge branch 'develop'Kenneth Reitzbefore adoption81d5997

lines 196–227why, one step back
196 >>> r.headers['Content-Type']
197 'application/json; charset=utf-8'198199 >>> r.headers.get('content-type')200 'application/json; charset=utf-8'201202If a header doesn't exist in the Response, its value defaults to ``None``::203204 >>> r.headers['X-Random']205 None206207208Cookies209-------210211If a response contains some Cookies, you can get quick access to them::212213 >>> url = 'http://httpbin.org/cookies/set/requests-is/awesome'214 >>> r = requests.get(url)215216 >>> print r.cookies217 {'requests-is': 'awesome'}218219To send your own cookies to the server, you can use the ``cookies``220parameter::221222 >>> url = 'http://httpbin.org/cookies'223 >>> cookies = dict(cookies_are='working')224225 >>> r = requests.get(url, cookies=cookies)226 >>> r.content227 '{"cookies": {"cookies_are": "working"}}'

Merge branch 'develop' into feature/rewriteKenneth Reitzbefore adoption95336a0

lines 228–235why, one step back
228
229230Basic Authentication231--------------------232233Most web services require authentication. There many different types of234authentication, but the most common is called HTTP Basic Auth.235

big quick start updatesKenneth Reitzbefore adoption60558fd

line 236why, one step back
236Making requests with Basic Auth is extremely simple::

Merge branch 'develop' into feature/rewriteKenneth Reitzbefore adoption95336a0

lines 237–244why, one step back
237
238 >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))239 <Response [200]>240241242Digest Authentication243---------------------244

big quick start updatesKenneth Reitzbefore adoption60558fd

line 245why, one step back
245Another popular form of protecting web service is Digest Authentication::

Merge branch 'develop' into feature/rewriteKenneth Reitzbefore adoption95336a0

lines 246–253why, one step back
246
247 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'248 >>> requests.get(url, auth=('digest', 'user', 'pass'))249 <Response [200]>250251252-----------------------253

Merge branch 'develop' into feature/rewriteKenneth Reitzbefore adoptionc48f0d9

line 254why, one step back
254Ready for more? Check out the :ref:`advanced <advanced>` section.