Skip to content
Myr

quickstart.rst

This file

Path

docs/user/quickstart.rst

494 lines · 13.8 KiB

Last changed by

typo all the thingsKenneth Reitzbefore adoption9840966

History of this file

Take it

The walk stopped after 200 commits: lines older than 0a37d82 point at it.

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 1–17why, one step back
1.. _quickstart:
23Quickstart4==========56.. module:: requests.models78Eager 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.1112First, make sure that:1314* Requests is :ref:`installed <install>`15* Requests is :ref:`up-to-date <updates>`1617

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 18why, one step back
18Let's get started with some simple examples.

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 19–20why, one step back
19
20

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 21why, one step back
21Make a Request

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

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

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

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

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 25why, one step back
25

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 26why, one step back
26Begin by importing the Requests module::

Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8

line 27why, one step back
27

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 28why, one step back
28 >>> import requests

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 29why, one step back
29

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 30–31why, one step back
30Now, let's try to get a webpage. For this example, let's get GitHub's public
31timeline ::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 32why, one step back
32

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 33–34why, one step back
33 >>> r = requests.get('https://github.com/timeline.json')
34

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 35why, one step back
35Now, we have a :class:`Response` object called ``r``. We can get all the

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 36why, one step back
36information we need from this object.

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 37why, one step back
37

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 38–39why, one step back
38Requests' simple API means that all forms of HTTP request are as obvious. For
39example, this is how you make an HTTP POST request::

Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8

line 40why, one step back
40

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

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

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 42why, one step back
42

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 43–44why, one step back
43Nice, right? What about the other HTTP request types: PUT, DELETE, HEAD and
44OPTIONS? These are all just as simple::

Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8

line 45why, one step back
45

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 46–65why, one step back
46 >>> r = requests.put("http://httpbin.org/put")
47 >>> r = requests.delete("http://httpbin.org/delete")48 >>> r = requests.head("http://httpbin.org/get")49 >>> r = requests.options("http://httpbin.org/get")5051That's all well and good, but it's also only the start of what Requests can52do.535455Passing Parameters In URLs56--------------------------5758You often want to send some sort of data in the URL's query string. If59you were constructing the URL by hand, this data would be given as key/value60pairs in the URL after a question mark, e.g. ``httpbin.org/get?key=val``.61Requests allows you to provide these arguments as a dictionary, using the62``params`` keyword argument. As an example, if you wanted to pass63``key1=value1`` and ``key2=value2`` to ``httpbin.org/get``, you would use the64following code::65

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 66–68why, one step back
66 >>> payload = {'key1': 'value1', 'key2': 'value2'}
67 >>> r = requests.get("http://httpbin.org/get", params=payload)68

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 69why, one step back
69You can see that the URL has been correctly encoded by printing the URL::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 70why, one step back
70

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 71–72why, one step back
71 >>> print r.url
72 u'http://httpbin.org/get?key2=value2&key1=value1'

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 73why, one step back
73

Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8

line 74why, one step back
74

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 75–77why, one step back
75Response Content
76----------------77

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 78–79why, one step back
78We can read the content of the server's response. Consider the GitHub timeline
79again::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 80why, one step back
80

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 81–82why, one step back
81 >>> import requests
82 >>> r = requests.get('https://github.com/timeline.json')

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 83–88why, one step back
83 >>> r.text
84 '[{"repository":{"open_issues":0,"url":"https://github.com/...8586Requests will automatically decode content from the server. Most unicode87charsets are seamlessly decoded.88

Merge pull request #772 from Lukasa/developKenneth Reitzbefore adoption6464618

lines 89–92why, one step back
89When you make a request, Requests makes educated guesses about the encoding of
90the response based on the HTTP headers. The text encoding guessed by Requests91is used when you access ``r.text``. You can find out what encoding Requests is92using, and change it, using the ``r.encoding`` property::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 93why, one step back
93

Merge pull request #772 from Lukasa/developKenneth Reitzbefore adoption6464618

lines 94–104why, one step back
94 >>> r.encoding
95 'utf-8'96 >>> r.encoding = 'ISO-8859-1'9798If you change the encoding, Requests will use the new value of ``r.encoding``99whenever you call ``r.text``.100101Requests will also use custom encodings in the event that you need them. If102you have created your own encoding and registered it with the ``codecs``103module, you can simply use the codec name as the value of ``r.encoding`` and104Requests will handle the decoding for you.

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 105–115why, one step back
105
106Binary Response Content107-----------------------108109You can also access the response body as bytes, for non-text requests::110111 >>> r.content112 b'[{"repository":{"open_issues":0,"url":"https://github.com/...113114The ``gzip`` and ``deflate`` transfer-encodings are automatically decoded for you.115

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 116why, one step back
116For example, to create an image from binary data returned by a request, you can

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 117–123why, one step back
117use the following code:
118119 >>> from PIL import Image120 >>> from StringIO import StringIO121 >>> i = Image.open(StringIO(r.content))122123

Added r.json documentation (document ALL the awesome features!)Danilo Bargenbefore adoption1164450

lines 124–136why, one step back
124JSON Response Content
125---------------------126127There's also a builtin JSON decoder, in case you're dealing with JSON data::128129 >>> import requests130 >>> r = requests.get('https://github.com/timeline.json')131 >>> r.json132 [{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...133134In case the JSON decoding fails, ``r.json`` simply returns ``None``.135136

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 137–149why, one step back
137Raw Response Content
138--------------------139140In the rare case that you'd like to get the absolute raw socket response from the server,141you can access ``r.raw``::142143 >>> r.raw144 <requests.packages.urllib3.response.HTTPResponse object at 0x101194810>145146 >>> r.raw.read(10)147 '\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'148149

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 150–151why, one step back
150Custom Headers
151--------------

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 152why, one step back
152

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 153–154why, one step back
153If you'd like to add HTTP headers to a request, simply pass in a ``dict`` to the
154``headers`` parameter.

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 155why, one step back
155

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 156why, one step back
156For example, we didn't specify our content-type in the previous example::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 157why, one step back
157

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 158–161why, one step back
158 >>> import json
159 >>> url = 'https://api.github.com/some/endpoint'160 >>> payload = {'some': 'data'}161 >>> headers = {'content-type': 'application/json'}

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 162why, one step back
162

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 163why, one step back
163 >>> r = requests.post(url, data=json.dumps(payload), headers=headers)

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 164why, one step back
164

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 165–168why, one step back
165
166More complicated POST requests167------------------------------168

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 169–176why, one step back
169Typically, you want to send some form-encoded data — much like an HTML form.
170To do this, simply pass a dictionary to the `data` argument. Your171dictionary of data will automatically be form-encoded when the request is made::172173 >>> payload = {'key1': 'value1', 'key2': 'value2'}174 >>> r = requests.post("http://httpbin.org/post", data=payload)175 >>> print r.text176 {

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 177why, one step back
177 // ...snip... //

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 178–181why, one step back
178 "form": {
179 "key2": "value2",180 "key1": "value1"181 },

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 182why, one step back
182 // ...snip... //

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 183–188why, one step back
183 }
184185There 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.186187For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data::188

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 189–191why, one step back
189 >>> import json
190 >>> url = 'https://api.github.com/some/endpoint'191 >>> payload = {'some': 'data'}

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 192why, one step back
192

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 193why, one step back
193 >>> r = requests.post(url, data=json.dumps(payload))

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 194–201why, one step back
194
195196POST a Multipart-Encoded File197-----------------------------198199Requests makes it simple to upload Multipart-encoded files::200201 >>> url = 'http://httpbin.org/post'

Merge pull request #752 from anentropic/patch-1Kenneth Reitzbefore adoption54842cd

line 202why, one step back
202 >>> files = {'file': open('report.xls', 'rb')}

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 203–206why, one step back
203
204 >>> r = requests.post(url, files=files)205 >>> r.text206 {

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 207why, one step back
207 // ...snip... //

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 208why, one step back
208 "files": {

Merge pull request #752 from anentropic/patch-1Kenneth Reitzbefore adoption54842cd

line 209why, one step back
209 "file": "<censored...binary...data>"

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 210why, one step back
210 },

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 211why, one step back
211 // ...snip... //

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 212–213why, one step back
212 }
213

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 214why, one step back
214You can set the filename explicitly::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 215–221why, one step back
215
216 >>> url = 'http://httpbin.org/post'217 >>> files = {'file': ('report.xls', open('report.xls', 'rb'))}218219 >>> r = requests.post(url, files=files)220 >>> r.text221 {

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 222why, one step back
222 // ...snip... //

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 223–225why, one step back
223 "files": {
224 "file": "<censored...binary...data>"225 },

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 226why, one step back
226 // ...snip... //

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 227–228why, one step back
227 }
228

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 229why, one step back
229If you want, you can send strings to be received as files::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 230–231why, one step back
230
231 >>> url = 'http://httpbin.org/post'

Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8

line 232why, one step back
232 >>> files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 233–236why, one step back
233
234 >>> r = requests.post(url, files=files)235 >>> r.text236 {

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 237why, one step back
237 // ...snip... //

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 238–240why, one step back
238 "files": {
239 "file": "some,data,to,send\\nanother,row,to,send\\n"240 },

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 241why, one step back
241 // ...snip... //

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 242–249why, one step back
242 }
243244245Response Status Codes246---------------------247248We can check the response status code::249

Merge remote-tracking branch 'origin/develop' into developKenneth Reitzbefore adoption6541ceb

line 250why, one step back
250 >>> r = requests.get('http://httpbin.org/get')

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 251–262why, one step back
251 >>> r.status_code
252 200253254Requests also comes with a built-in status code lookup object for easy255reference::256257 >>> r.status_code == requests.codes.ok258 True259260If we made a bad request (non-200 response), we can raise it with261:class:`Response.raise_for_status()`::262

Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8

lines 263–264why, one step back
263 >>> bad_r = requests.get('http://httpbin.org/status/404')
264 >>> bad_r.status_code

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 265–266why, one step back
265 404
266

Merge pull request #784 from Locker537/docs_fixKenneth Reitzbefore adoptioncb0fdd8

line 267why, one step back
267 >>> bad_r.raise_for_status()

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 268why, one step back
268 Traceback (most recent call last):

Merge pull request #683 from jkleint/patch-1Kenneth Reitzbefore adoption38db8c2

lines 269–271why, one step back
269 File "requests/models.py", line 832, in raise_for_status
270 raise http_error271 requests.exceptions.HTTPError: 404 Client Error

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 272why, one step back
272

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 273–274why, one step back
273But, since our ``status_code`` for ``r`` was ``200``, when we call
274``raise_for_status()`` we get::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 275–284why, one step back
275
276 >>> r.raise_for_status()277 None278279All is well.280281282Response Headers283----------------284

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 285why, one step back
285We can view the server's response headers using a Python dictionary::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 286–341why, one step back
286
287 >>> r.headers288 {289 'status': '200 OK',290 'content-encoding': 'gzip',291 'transfer-encoding': 'chunked',292 'connection': 'close',293 'server': 'nginx/1.0.4',294 'x-runtime': '148ms',295 'etag': '"e1ca502697e5c9317743dc078f67693f"',296 'content-type': 'application/json; charset=utf-8'297 }298299The dictionary is special, though: it's made just for HTTP headers. According to300`RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTP301Headers are case-insensitive.302303So, we can access the headers using any capitalization we want::304305 >>> r.headers['Content-Type']306 'application/json; charset=utf-8'307308 >>> r.headers.get('content-type')309 'application/json; charset=utf-8'310311If a header doesn't exist in the Response, its value defaults to ``None``::312313 >>> r.headers['X-Random']314 None315316317Cookies318-------319320If a response contains some Cookies, you can get quick access to them::321322 >>> url = 'http://httpbin.org/cookies/set/requests-is/awesome'323 >>> r = requests.get(url)324325 >>> r.cookies['requests-is']326 'awesome'327328To send your own cookies to the server, you can use the ``cookies``329parameter::330331 >>> url = 'http://httpbin.org/cookies'332 >>> cookies = dict(cookies_are='working')333334 >>> r = requests.get(url, cookies=cookies)335 >>> r.text336 '{"cookies": {"cookies_are": "working"}}'337338339Basic Authentication340--------------------341

Merge pull request #787 from kayzh/developKenneth Reitzbefore adoptionbbf1946

line 342why, one step back
342Many web services require authentication. There are many different types of

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 343–374why, one step back
343authentication, but the most common is HTTP Basic Auth.
344345Making requests with Basic Auth is extremely simple::346347 >>> from requests.auth import HTTPBasicAuth348 >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))349 <Response [200]>350351Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for352this authentication method::353354 >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))355 <Response [200]>356357Providing the credentials as a tuple in this fashion is functionally equivalent358to the ``HTTPBasicAuth`` example above.359360361Digest Authentication362---------------------363364Another popular form of web service protection is Digest Authentication::365366 >>> from requests.auth import HTTPDigestAuth367 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'368 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))369 <Response [200]>370371372OAuth Authentication373--------------------374

oauth awesomenessKenneth Reitzbefore adoptionf33a0aa

lines 375–376why, one step back
375Requests features robust, built-in OAuth support!
376

typo all the thingsKenneth Reitzbefore adoption9840966

line 377why, one step back
377OAuth takes many forms, so let's take a look at a few different forms::

oauth awesomenessKenneth Reitzbefore adoptionf33a0aa

lines 378–410why, one step back
378
379 import requests380 from requests.auth import OAuth1381382 url = u'https://api.twitter.com/1/account/settings.json'383384 client_key = u'...'385 client_secret = u'...'386 resource_owner_key = u'...'387 resource_owner_secret = u'...'388389390Query signing::391392 queryoauth = OAuth1(client_key, client_secret,393 resource_owner_key, resource_owner_secret,394 signature_type='query')395 r = requests.get(url, auth=queryoauth)396397Header signing::398399 headeroauth = OAuth1(client_key, client_secret,400 resource_owner_key, resource_owner_secret,401 signature_type='auth_header')402 r = requests.get(url, auth=headeroauth)403404Body signing::405406 bodyoauth = OAuth1(client_key, client_secret,407 resource_owner_key, resource_owner_secret,408 signature_type='body')409410 r = requests.post(url, auth=bodyoauth)

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 411–415why, one step back
411
412413Redirection and History414-----------------------415

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 416–417why, one step back
416Requests will automatically perform location redirection while using the GET
417and OPTIONS verbs.

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 418why, one step back
418

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 419–420why, one step back
419GitHub redirects all HTTP requests to HTTPS. We can use the ``history`` method
420of the Response object to track redirection. Let's see what Github does::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 421–430why, one step back
421
422 >>> r = requests.get('http://github.com')423 >>> r.url424 'https://github.com/'425 >>> r.status_code426 200427 >>> r.history428 [<Response [301]>]429430The :class:`Response.history` list contains a list of the

Merge pull request #721 from karlcow/developKenneth Reitzbefore adoptionc3ab38e

line 431why, one step back
431:class:`Request` objects that were created in order to complete the request. The list is sorted from the oldest to the most recent request.

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 432why, one step back
432

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 433–434why, one step back
433If you're using GET or OPTIONS, you can disable redirection handling with the
434``allow_redirects`` parameter::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 435–441why, one step back
435
436 >>> r = requests.get('http://github.com', allow_redirects=False)437 >>> r.status_code438 301439 >>> r.history440 []441

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 442–443why, one step back
442If you're using POST, PUT, PATCH, DELETE or HEAD, you can enable
443redirection as well::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 444–454why, one step back
444
445 >>> r = requests.post('http://github.com', allow_redirects=True)446 >>> r.url447 'https://github.com/'448 >>> r.history449 [<Response [301]>]450451452Timeouts453--------454

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 455–456why, one step back
455You can tell requests to stop waiting for a response after a given number of
456seconds with the ``timeout`` parameter::

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 457–462why, one step back
457
458 >>> requests.get('http://github.com', timeout=0.001)459 Traceback (most recent call last):460 File "<stdin>", line 1, in <module>461 requests.exceptions.Timeout: Request timed out.462

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

line 463why, one step back
463.. admonition:: Note:

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

line 464why, one step back
464

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 465–466why, one step back
465 ``timeout`` only effects the connection process itself, not the
466 downloading of the response body.

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 467–479why, one step back
467
468469Errors and Exceptions470---------------------471472In the event of a network problem (e.g. DNS failure, refused connection, etc),473Requests will raise a :class:`ConnectionError` exception.474475In the event of the rare invalid HTTP response, Requests will raise476an :class:`HTTPError` exception.477478If a request times out, a :class:`Timeout` exception is raised.479

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 480–481why, one step back
480If a request exceeds the configured number of maximum redirections, a
481:class:`TooManyRedirects` exception is raised.

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 482–485why, one step back
482
483All exceptions that Requests explicitly raises inherit from484:class:`requests.exceptions.RequestException`.485

Merge pull request #619 from Lukasa/developKenneth Reitzbefore adoptionad8205f

lines 486–490why, one step back
486You can refer to :ref:`Configuration API Docs <configurations>` for immediate
487raising of :class:`HTTPError` exceptions via the ``danger_mode`` option or488have Requests catch the majority of489:class:`requests.exceptions.RequestException` exceptions with the ``safe_mode``490option.

Merge pull request #603 from benmao/developKenneth Reitzbefore adoption0a37d82

lines 491–494why, one step back
491
492-----------------------493494Ready for more? Check out the :ref:`advanced <advanced>` section.