api.rst
This file
Path
docs/api.rst
267 lines · 7.1 KiB
Last changed by
Merge pull request #2713 from lukasgraf/masterIan Cordascobefore adoption408d75d
History of this fileTake it
The walk stopped after 200 commits: lines older than e25fbc8 point at it.
Merge pull request #2135 from dpursehouse/fix-meth-and-class-directivesCory Benfieldbefore adoptione25fbc8
lines 1–7why, one step back1.. _api:23Developer Interface4===================56.. module:: requests7Merge pull request #2176 from kevinburke/connectKenneth Reitzbefore adoption32e403a
line 8why, one step back8This part of the documentation covers all the interfaces of Requests. ForMerge pull request #2135 from dpursehouse/fix-meth-and-class-directivesCory Benfieldbefore adoptione25fbc8
lines 9–46why, one step back9parts where Requests depends on external libraries, we document the most10important right here and provide links to the canonical documentation.111213Main Interface14--------------1516All of Requests' functionality can be accessed by these 7 methods.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:4344.. autoclass:: requests.adapters.HTTPAdapter45 :inherited-members:46Merge pull request #2138 from dpursehouse/more-doc-updatesCory Benfieldbefore adoption587a515
lines 47–53why, one step back47Authentication48--------------4950.. autoclass:: requests.auth.AuthBase51.. autoclass:: requests.auth.HTTPBasicAuth52.. autoclass:: requests.auth.HTTPProxyAuth53.. autoclass:: requests.auth.HTTPDigestAuthMerge pull request #2135 from dpursehouse/fix-meth-and-class-directivesCory Benfieldbefore adoptione25fbc8
lines 54–62why, one step back5455Exceptions56~~~~~~~~~~5758.. autoexception:: requests.exceptions.RequestException59.. autoexception:: requests.exceptions.ConnectionError60.. autoexception:: requests.exceptions.HTTPError61.. autoexception:: requests.exceptions.URLRequired62.. autoexception:: requests.exceptions.TooManyRedirectsMerge pull request #2187 from kevinburke/more-docsKenneth Reitzbefore adoption07bbe21
lines 63–64why, one step back63.. autoexception:: requests.exceptions.ConnectTimeout64.. autoexception:: requests.exceptions.ReadTimeoutMerge pull request #2135 from dpursehouse/fix-meth-and-class-directivesCory Benfieldbefore adoptione25fbc8
lines 65–83why, one step back65.. autoexception:: requests.exceptions.Timeout666768Status Code Lookup69~~~~~~~~~~~~~~~~~~7071.. autofunction:: requests.codes7273::7475 >>> requests.codes['temporary_redirect']76 3077778 >>> requests.codes.teapot79 4188081 >>> requests.codes['\o/']82 20083Merge pull request #2713 from lukasgraf/masterIan Cordascobefore adoption408d75d
lines 84–85why, one step back84.. _api-cookies:85Merge pull request #2135 from dpursehouse/fix-meth-and-class-directivesCory Benfieldbefore adoptione25fbc8
lines 86–92why, one step back86Cookies87~~~~~~~8889.. autofunction:: requests.utils.dict_from_cookiejar90.. autofunction:: requests.utils.cookiejar_from_dict91.. autofunction:: requests.utils.add_dict_to_cookiejar92Merge pull request #2390 from Lukasa/cookiedocsKenneth Reitzbefore adoption2d1ffad
lines 93–98why, one step back93.. autoclass:: requests.cookies.RequestsCookieJar94 :inherited-members:9596.. autoclass:: requests.cookies.CookieConflictError97 :inherited-members:98Merge pull request #2135 from dpursehouse/fix-meth-and-class-directivesCory Benfieldbefore adoptione25fbc8
lines 99–259why, one step back99100Encodings101~~~~~~~~~102103.. autofunction:: requests.utils.get_encodings_from_content104.. autofunction:: requests.utils.get_encoding_from_headers105.. autofunction:: requests.utils.get_unicode_from_response106107108Classes109~~~~~~~110111.. autoclass:: requests.Response112 :inherited-members:113114.. autoclass:: requests.Request115 :inherited-members:116117.. autoclass:: requests.PreparedRequest118 :inherited-members:119120.. _sessionapi:121122.. autoclass:: requests.Session123 :inherited-members:124125.. autoclass:: requests.adapters.HTTPAdapter126 :inherited-members:127128129Migrating to 1.x130----------------131132This section details the main differences between 0.x and 1.x and is meant133to ease the pain of upgrading.134135136API Changes137~~~~~~~~~~~138139* ``Response.json`` is now a callable and not a property of a response.140141 ::142143 import requests144 r = requests.get('https://github.com/timeline.json')145 r.json() # This *call* raises an exception if JSON decoding fails146147* The ``Session`` API has changed. Sessions objects no longer take parameters.148 ``Session`` is also now capitalized, but it can still be149 instantiated with a lowercase ``session`` for backwards compatibility.150151 ::152153 s = requests.Session() # formerly, session took parameters154 s.auth = auth155 s.headers.update(headers)156 r = s.get('http://httpbin.org/headers')157158* All request hooks have been removed except 'response'.159160* Authentication helpers have been broken out into separate modules. See161 requests-oauthlib_ and requests-kerberos_.162163.. _requests-oauthlib: https://github.com/requests/requests-oauthlib164.. _requests-kerberos: https://github.com/requests/requests-kerberos165166* The parameter for streaming requests was changed from ``prefetch`` to167 ``stream`` and the logic was inverted. In addition, ``stream`` is now168 required for raw response reading.169170 ::171172 # in 0.x, passing prefetch=False would accomplish the same thing173 r = requests.get('https://github.com/timeline.json', stream=True)174 for chunk in r.iter_content(8192):175 ...176177* The ``config`` parameter to the requests method has been removed. Some of178 these options are now configured on a ``Session`` such as keep-alive and179 maximum number of redirects. The verbosity option should be handled by180 configuring logging.181182 ::183184 import requests185 import logging186187 # these two lines enable debugging at httplib level (requests->urllib3->httplib)188 # you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.189 # the only thing missing will be the response.body which is not logged.190 import httplib191 httplib.HTTPConnection.debuglevel = 1192193 logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests194 logging.getLogger().setLevel(logging.DEBUG)195 requests_log = logging.getLogger("requests.packages.urllib3")196 requests_log.setLevel(logging.DEBUG)197 requests_log.propagate = True198199 requests.get('http://httpbin.org/headers')200201202203Licensing204~~~~~~~~~205206One key difference that has nothing to do with the API is a change in the207license from the ISC_ license to the `Apache 2.0`_ license. The Apache 2.0208license ensures that contributions to Requests are also covered by the Apache2092.0 license.210211.. _ISC: http://opensource.org/licenses/ISC212.. _Apache 2.0: http://opensource.org/licenses/Apache-2.0213214215Migrating to 2.x216----------------217218219Compared with the 1.0 release, there were relatively few backwards220incompatible changes, but there are still a few issues to be aware of with221this major release.222223For more details on the changes in this release including new APIs, links224to the relevant GitHub issues and some of the bug fixes, read Cory's blog_225on the subject.226227.. _blog: http://lukasa.co.uk/2013/09/Requests_20/228229230API Changes231~~~~~~~~~~~232233* There were a couple changes to how Requests handles exceptions.234 ``RequestException`` is now a subclass of ``IOError`` rather than235 ``RuntimeError`` as that more accurately categorizes the type of error.236 In addition, an invalid URL escape sequence now raises a subclass of237 ``RequestException`` rather than a ``ValueError``.238239 ::240241 requests.get('http://%zz/') # raises requests.exceptions.InvalidURL242243 Lastly, ``httplib.IncompleteRead`` exceptions caused by incorrect chunked244 encoding will now raise a Requests ``ChunkedEncodingError`` instead.245246* The proxy API has changed slightly. The scheme for a proxy URL is now247 required.248249 ::250251 proxies = {252 "http": "10.10.1.10:3128", # use http://10.10.1.10:3128 instead253 }254255 # In requests 1.x, this was legal, in requests 2.x,256 # this raises requests.exceptions.MissingSchema257 requests.get("http://example.org", proxies=proxies)258259Merge pull request #2187 from kevinburke/more-docsKenneth Reitzbefore adoption07bbe21
lines 260–261why, one step back260Behavioural Changes261~~~~~~~~~~~~~~~~~~~~~~~Merge pull request #2135 from dpursehouse/fix-meth-and-class-directivesCory Benfieldbefore adoptione25fbc8
lines 262–267why, one step back262263* Keys in the ``headers`` dictionary are now native strings on all Python264 versions, i.e. bytestrings on Python 2 and unicode on Python 3. If the265 keys are not native strings (unicode on Python2 or bytestrings on Python 3)266 they will be converted to the native string type assuming UTF-8 encoding.267