Skip to content
Myr

api.rst

This file

Path

docs/api.rst

265 lines · 7.2 KiB

Last changed by

Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption011f6da

History of this file

Take it

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

Merge pull request #1161 from sigmavirus24/fix1159Kenneth Reitzbefore adoptionf2be5fa

lines 1–15why, one step back
1.. _api:
23Developer Interface4===================56.. module:: requests78This part of the documentation covers all the interfaces of Requests. For9parts where Requests depends on external libraries, we document the most10important right here and provide links to the canonical documentation.111213Main Interface14--------------15

Merge pull request #1330 from Kwpolska/patch-1Kenneth Reitzbefore adoptiond2ba579

line 16why, one step back
16All of Requests' functionality can be accessed by these 7 methods.

Merge pull request #1161 from sigmavirus24/fix1159Kenneth Reitzbefore adoptionf2be5fa

lines 17–43why, one step back
17They all return an instance of the :class:`Response <Response>` object.
1819.. autofunction:: request2021.. autofunction:: head22.. autofunction:: get23.. autofunction:: post24.. autofunction:: put25.. autofunction:: patch26.. autofunction:: delete272829Lower-Level Classes30~~~~~~~~~~~~~~~~~~~3132.. autoclass:: requests.Request33 :inherited-members:3435.. autoclass:: Response36 :inherited-members:3738Request Sessions39----------------4041.. autoclass:: Session42 :inherited-members:43

Merge pull request #1298 from Lukasa/adapter_docKenneth Reitzbefore adoption2426eeb

lines 44–45why, one step back
44.. autoclass:: requests.adapters.HTTPAdapter
45 :inherited-members:

Merge pull request #1161 from sigmavirus24/fix1159Kenneth Reitzbefore adoptionf2be5fa

line 46why, one step back
46

Merge pull request #1298 from Lukasa/adapter_docKenneth Reitzbefore adoption2426eeb

line 47why, one step back
47

Merge pull request #1161 from sigmavirus24/fix1159Kenneth Reitzbefore adoptionf2be5fa

lines 48–50why, one step back
48Exceptions
49~~~~~~~~~~50

Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption7430a49

lines 51–55why, one step back
51.. autoexception:: requests.exceptions.RequestException
52.. autoexception:: requests.exceptions.ConnectionError53.. autoexception:: requests.exceptions.HTTPError54.. autoexception:: requests.exceptions.URLRequired55.. autoexception:: requests.exceptions.TooManyRedirects

Merge pull request #1161 from sigmavirus24/fix1159Kenneth Reitzbefore adoptionf2be5fa

lines 56–76why, one step back
56
5758Status Code Lookup59~~~~~~~~~~~~~~~~~~6061.. autofunction:: requests.codes6263::6465 >>> requests.codes['temporary_redirect']66 3076768 >>> requests.codes.teapot69 4187071 >>> requests.codes['\o/']72 2007374Cookies75~~~~~~~76

Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption7430a49

lines 77–79why, one step back
77.. autofunction:: requests.utils.dict_from_cookiejar
78.. autofunction:: requests.utils.cookiejar_from_dict79.. autofunction:: requests.utils.add_dict_to_cookiejar

Merge pull request #1161 from sigmavirus24/fix1159Kenneth Reitzbefore adoptionf2be5fa

lines 80–84why, one step back
80
8182Encodings83~~~~~~~~~84

Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption7430a49

lines 85–87why, one step back
85.. autofunction:: requests.utils.get_encodings_from_content
86.. autofunction:: requests.utils.get_encoding_from_headers87.. autofunction:: requests.utils.get_unicode_from_response

Merge pull request #1161 from sigmavirus24/fix1159Kenneth Reitzbefore adoptionf2be5fa

lines 88–104why, one step back
88
8990Classes91~~~~~~~9293.. autoclass:: requests.Response94 :inherited-members:9596.. autoclass:: requests.Request97 :inherited-members:9899.. autoclass:: requests.PreparedRequest100 :inherited-members:101102.. _sessionapi:103104.. autoclass:: requests.Session

Merge pull request #1298 from Lukasa/adapter_docKenneth Reitzbefore adoption2426eeb

lines 105–107why, one step back
105 :inherited-members:
106107.. autoclass:: requests.adapters.HTTPAdapter

Merge pull request #1161 from sigmavirus24/fix1159Kenneth Reitzbefore adoptionf2be5fa

lines 108–109why, one step back
108 :inherited-members:
109

Merge pull request #1231 from davidfischer/docs-migration1.0Kenneth Reitzbefore adoption08cab48

lines 110–155why, one step back
110
111Migrating to 1.x112----------------113114This section details the main differences between 0.x and 1.x and is meant115to ease the pain of upgrading.116117118API Changes119~~~~~~~~~~~120121* ``Response.json`` is now a callable and not a property of a response.122123 ::124125 import requests126 r = requests.get('https://github.com/timeline.json')127 r.json() # This *call* raises an exception if JSON decoding fails128129* The ``Session`` API has changed. Sessions objects no longer take parameters.130 ``Session`` is also now capitalized, but it can still be131 instantiated with a lowercase ``session`` for backwards compatibility.132133 ::134135 s = requests.Session() # formerly, session took parameters136 s.auth = auth137 s.headers.update(headers)138 r = s.get('http://httpbin.org/headers')139140* All request hooks have been removed except 'response'.141142* Authentication helpers have been broken out into separate modules. See143 requests-oauthlib_ and requests-kerberos_.144145.. _requests-oauthlib: https://github.com/requests/requests-oauthlib146.. _requests-kerberos: https://github.com/requests/requests-kerberos147148* The parameter for streaming requests was changed from ``prefetch`` to149 ``stream`` and the logic was inverted. In addition, ``stream`` is now150 required for raw response reading.151152 ::153154 # in 0.x, passing prefetch=False would accomplish the same thing155 r = requests.get('https://github.com/timeline.json', stream=True)

Merge pull request #1592 from homm/masterCory Benfieldbefore adoptiond8f2463

lines 156–157why, one step back
156 for chunk in r.iter_content(8192):
157 ...

Merge pull request #1231 from davidfischer/docs-migration1.0Kenneth Reitzbefore adoption08cab48

lines 158–165why, one step back
158
159* The ``config`` parameter to the requests method has been removed. Some of160 these options are now configured on a ``Session`` such as keep-alive and161 maximum number of redirects. The verbosity option should be handled by162 configuring logging.163164 ::165

Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption636476e

lines 166–173why, one step back
166 import requests
167 import logging168169 # these two lines enable debugging at httplib level (requests->urllib3->httplib)170 # you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.171 # the only thing missing will be the response.body which is not logged.172 import httplib173 httplib.HTTPConnection.debuglevel = 1

Merge pull request #1592 from homm/masterCory Benfieldbefore adoptiond8f2463

line 174why, one step back
174

Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption636476e

lines 175–179why, one step back
175 logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests
176 logging.getLogger().setLevel(logging.DEBUG)177 requests_log = logging.getLogger("requests.packages.urllib3")178 requests_log.setLevel(logging.DEBUG)179 requests_log.propagate = True

Merge pull request #1592 from homm/masterCory Benfieldbefore adoptiond8f2463

line 180why, one step back
180

Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption636476e

lines 181–182why, one step back
181 requests.get('http://httpbin.org/headers')
182

Merge pull request #1231 from davidfischer/docs-migration1.0Kenneth Reitzbefore adoption08cab48

lines 183–189why, one step back
183
184185Licensing186~~~~~~~~~187188One key difference that has nothing to do with the API is a change in the189license from the ISC_ license to the `Apache 2.0`_ license. The Apache 2.0

Merge pull request #1474 from Kwpolska/docfixKenneth Reitzbefore adoptiond83919a

line 190why, one step back
190license ensures that contributions to Requests are also covered by the Apache

Merge pull request #1231 from davidfischer/docs-migration1.0Kenneth Reitzbefore adoption08cab48

lines 191–195why, one step back
1912.0 license.
192193.. _ISC: http://opensource.org/licenses/ISC194.. _Apache 2.0: http://opensource.org/licenses/Apache-2.0195

Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption011f6da

lines 196–265why, one step back
196
197Migrating to 2.x198----------------199200201Compared with the 1.0 release, there were relatively few backwards202incompatible changes, but there are still a few issues to be aware of with203this major release.204205For more details on the changes in this release including new APIs, links206to the relevant GitHub issues and some of the bug fixes, read Cory's blog_207on the subject.208209.. _blog: http://lukasa.co.uk/2013/09/Requests_20/210211212API Changes213~~~~~~~~~~~214215* There were a couple changes to how Requests handles exceptions.216 ``RequestException`` is now a subclass of ``IOError`` rather than217 ``RuntimeError`` as that more accurately categorizes the type of error.218 In addition, an invalid URL escape sequence now raises a subclass of219 ``RequestException`` rather than a ``ValueError``.220221 ::222223 requests.get('http://%zz/') # raises requests.exceptions.InvalidURL224225 Lastly, ``httplib.IncompleteRead`` exceptions caused by incorrect chunked226 encoding will now raise a Requests ``ChunkedEncodingError`` instead.227228* The proxy API has changed slightly. The scheme for a proxy URL is now229 required.230231 ::232233 proxies = {234 "http": "10.10.1.10:3128", # use http://10.10.1.10:3128 instead235 }236237 # In requests 1.x, this was legal, in requests 2.x,238 # this raises requests.exceptions.MissingSchema239 requests.get("http://example.org", proxies=proxies)240241242Behavioral Changes243~~~~~~~~~~~~~~~~~~244245* Keys in the ``headers`` dictionary are now native strings on all Python246 versions, i.e. bytestrings on Python 2 and unicode on Python 3. If the247 keys are not native strings (unicode on Python2 or bytestrings on Python 3)248 they will be converted to the native string type assuming UTF-8 encoding.249250* Timeouts behave slightly differently. On streaming requests, the timeout251 only applies to the connection attempt. On regular requests, the timeout252 is applied to the connection process and downloading the full body.253254 ::255256 tarball_url = 'https://github.com/kennethreitz/requests/tarball/master'257258 # One second timeout for the connection attempt259 # Unlimited time to download the tarball260 r = requests.get(tarball_url, stream=True, timeout=1)261262 # One second timeout for the connection attempt263 # Another full second timeout to download the tarball264 r = requests.get(tarball_url, timeout=1)265