Skip to content
Myr

api.rst

This file

Path

docs/api.rst

265 lines · 7.1 KiB

Last changed by

Merge pull request #2390 from Lukasa/cookiedocsKenneth Reitzbefore adoption2d1ffad

History of this file

Take it

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

Update changelog for 2.2.1.Cory Benfieldbefore adoption2a6b835

lines 1–7why, one step back
1.. _api:
23Developer Interface4===================56.. module:: requests7

Merge pull request #2176 from kevinburke/connectKenneth Reitzbefore adoption32e403a

line 8why, one step back
8This part of the documentation covers all the interfaces of Requests. For

Update changelog for 2.2.1.Cory Benfieldbefore adoption2a6b835

lines 9–46why, one step back
9parts where Requests depends on external libraries, we document the most
10important 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:46

Merge pull request #2138 from dpursehouse/more-doc-updatesCory Benfieldbefore adoption587a515

lines 47–53why, one step back
47Authentication
48--------------4950.. autoclass:: requests.auth.AuthBase51.. autoclass:: requests.auth.HTTPBasicAuth52.. autoclass:: requests.auth.HTTPProxyAuth53.. autoclass:: requests.auth.HTTPDigestAuth

Update changelog for 2.2.1.Cory Benfieldbefore adoption2a6b835

lines 54–62why, one step back
54
55Exceptions56~~~~~~~~~~5758.. autoexception:: requests.exceptions.RequestException59.. autoexception:: requests.exceptions.ConnectionError60.. autoexception:: requests.exceptions.HTTPError61.. autoexception:: requests.exceptions.URLRequired62.. autoexception:: requests.exceptions.TooManyRedirects

Merge pull request #2187 from kevinburke/more-docsKenneth Reitzbefore adoption07bbe21

lines 63–64why, one step back
63.. autoexception:: requests.exceptions.ConnectTimeout
64.. autoexception:: requests.exceptions.ReadTimeout

Merge pull request #2131 from dpursehouse/quickstart-doc-updatesCory Benfieldbefore adoption127ab0a

line 65why, one step back
65.. autoexception:: requests.exceptions.Timeout

Update changelog for 2.2.1.Cory Benfieldbefore adoption2a6b835

lines 66–90why, one step back
66
6768Status Code Lookup69~~~~~~~~~~~~~~~~~~7071.. autofunction:: requests.codes7273::7475 >>> requests.codes['temporary_redirect']76 3077778 >>> requests.codes.teapot79 4188081 >>> requests.codes['\o/']82 2008384Cookies85~~~~~~~8687.. autofunction:: requests.utils.dict_from_cookiejar88.. autofunction:: requests.utils.cookiejar_from_dict89.. autofunction:: requests.utils.add_dict_to_cookiejar90

Merge pull request #2390 from Lukasa/cookiedocsKenneth Reitzbefore adoption2d1ffad

lines 91–96why, one step back
91.. autoclass:: requests.cookies.RequestsCookieJar
92 :inherited-members:9394.. autoclass:: requests.cookies.CookieConflictError95 :inherited-members:96

Update changelog for 2.2.1.Cory Benfieldbefore adoption2a6b835

lines 97–257why, one step back
97
98Encodings99~~~~~~~~~100101.. autofunction:: requests.utils.get_encodings_from_content102.. autofunction:: requests.utils.get_encoding_from_headers103.. autofunction:: requests.utils.get_unicode_from_response104105106Classes107~~~~~~~108109.. autoclass:: requests.Response110 :inherited-members:111112.. autoclass:: requests.Request113 :inherited-members:114115.. autoclass:: requests.PreparedRequest116 :inherited-members:117118.. _sessionapi:119120.. autoclass:: requests.Session121 :inherited-members:122123.. autoclass:: requests.adapters.HTTPAdapter124 :inherited-members:125126127Migrating to 1.x128----------------129130This section details the main differences between 0.x and 1.x and is meant131to ease the pain of upgrading.132133134API Changes135~~~~~~~~~~~136137* ``Response.json`` is now a callable and not a property of a response.138139 ::140141 import requests142 r = requests.get('https://github.com/timeline.json')143 r.json() # This *call* raises an exception if JSON decoding fails144145* The ``Session`` API has changed. Sessions objects no longer take parameters.146 ``Session`` is also now capitalized, but it can still be147 instantiated with a lowercase ``session`` for backwards compatibility.148149 ::150151 s = requests.Session() # formerly, session took parameters152 s.auth = auth153 s.headers.update(headers)154 r = s.get('http://httpbin.org/headers')155156* All request hooks have been removed except 'response'.157158* Authentication helpers have been broken out into separate modules. See159 requests-oauthlib_ and requests-kerberos_.160161.. _requests-oauthlib: https://github.com/requests/requests-oauthlib162.. _requests-kerberos: https://github.com/requests/requests-kerberos163164* The parameter for streaming requests was changed from ``prefetch`` to165 ``stream`` and the logic was inverted. In addition, ``stream`` is now166 required for raw response reading.167168 ::169170 # in 0.x, passing prefetch=False would accomplish the same thing171 r = requests.get('https://github.com/timeline.json', stream=True)172 for chunk in r.iter_content(8192):173 ...174175* The ``config`` parameter to the requests method has been removed. Some of176 these options are now configured on a ``Session`` such as keep-alive and177 maximum number of redirects. The verbosity option should be handled by178 configuring logging.179180 ::181182 import requests183 import logging184185 # these two lines enable debugging at httplib level (requests->urllib3->httplib)186 # you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.187 # the only thing missing will be the response.body which is not logged.188 import httplib189 httplib.HTTPConnection.debuglevel = 1190191 logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests192 logging.getLogger().setLevel(logging.DEBUG)193 requests_log = logging.getLogger("requests.packages.urllib3")194 requests_log.setLevel(logging.DEBUG)195 requests_log.propagate = True196197 requests.get('http://httpbin.org/headers')198199200201Licensing202~~~~~~~~~203204One key difference that has nothing to do with the API is a change in the205license from the ISC_ license to the `Apache 2.0`_ license. The Apache 2.0206license ensures that contributions to Requests are also covered by the Apache2072.0 license.208209.. _ISC: http://opensource.org/licenses/ISC210.. _Apache 2.0: http://opensource.org/licenses/Apache-2.0211212213Migrating to 2.x214----------------215216217Compared with the 1.0 release, there were relatively few backwards218incompatible changes, but there are still a few issues to be aware of with219this major release.220221For more details on the changes in this release including new APIs, links222to the relevant GitHub issues and some of the bug fixes, read Cory's blog_223on the subject.224225.. _blog: http://lukasa.co.uk/2013/09/Requests_20/226227228API Changes229~~~~~~~~~~~230231* There were a couple changes to how Requests handles exceptions.232 ``RequestException`` is now a subclass of ``IOError`` rather than233 ``RuntimeError`` as that more accurately categorizes the type of error.234 In addition, an invalid URL escape sequence now raises a subclass of235 ``RequestException`` rather than a ``ValueError``.236237 ::238239 requests.get('http://%zz/') # raises requests.exceptions.InvalidURL240241 Lastly, ``httplib.IncompleteRead`` exceptions caused by incorrect chunked242 encoding will now raise a Requests ``ChunkedEncodingError`` instead.243244* The proxy API has changed slightly. The scheme for a proxy URL is now245 required.246247 ::248249 proxies = {250 "http": "10.10.1.10:3128", # use http://10.10.1.10:3128 instead251 }252253 # In requests 1.x, this was legal, in requests 2.x,254 # this raises requests.exceptions.MissingSchema255 requests.get("http://example.org", proxies=proxies)256257

Merge pull request #2187 from kevinburke/more-docsKenneth Reitzbefore adoption07bbe21

lines 258–259why, one step back
258Behavioural Changes
259~~~~~~~~~~~~~~~~~~~~~~~

Update changelog for 2.2.1.Cory Benfieldbefore adoption2a6b835

lines 260–265why, one step back
260
261* Keys in the ``headers`` dictionary are now native strings on all Python262 versions, i.e. bytestrings on Python 2 and unicode on Python 3. If the263 keys are not native strings (unicode on Python2 or bytestrings on Python 3)264 they will be converted to the native string type assuming UTF-8 encoding.265