api.rst
This file
Path
docs/api.rst
197 lines · 4.7 KiB
Last changed by
Merge remote-tracking branch 'origin/master'Kenneth Reitzbefore adoption636476e
History of this fileTake it
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--------------1516All of Request's 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:464748Exceptions49~~~~~~~~~~5051.. module:: requests5253.. autoexception:: RequestException54.. autoexception:: ConnectionError55.. autoexception:: HTTPError56.. autoexception:: URLRequired57.. autoexception:: TooManyRedirects585960Status Code Lookup61~~~~~~~~~~~~~~~~~~6263.. autofunction:: requests.codes6465::6667 >>> requests.codes['temporary_redirect']68 3076970 >>> requests.codes.teapot71 4187273 >>> requests.codes['\o/']74 2007576Cookies77~~~~~~~7879.. autofunction:: dict_from_cookiejar80.. autofunction:: cookiejar_from_dict81.. autofunction:: add_dict_to_cookiejar828384Encodings85~~~~~~~~~8687.. autofunction:: get_encodings_from_content88.. autofunction:: get_encoding_from_headers89.. autofunction:: get_unicode_from_response90.. autofunction:: decode_gzip919293Classes94~~~~~~~9596.. autoclass:: requests.Response97 :inherited-members:9899.. autoclass:: requests.Request100 :inherited-members:101102.. autoclass:: requests.PreparedRequest103 :inherited-members:104105.. _sessionapi:106107.. autoclass:: requests.Session108 :inherited-members:109110.. autoclass:: requests.adapters.HTTPAdapter111 :inherited-members:112113114Migrating to 1.x115----------------116117This section details the main differences between 0.x and 1.x and is meant118to ease the pain of upgrading.119120121API Changes122~~~~~~~~~~~123124* ``Response.json`` is now a callable and not a property of a response.125126 ::127128 import requests129 r = requests.get('https://github.com/timeline.json')130 r.json() # This *call* raises an exception if JSON decoding fails131132* The ``Session`` API has changed. Sessions objects no longer take parameters.133 ``Session`` is also now capitalized, but it can still be134 instantiated with a lowercase ``session`` for backwards compatibility.135136 ::137138 s = requests.Session() # formerly, session took parameters139 s.auth = auth140 s.headers.update(headers)141 r = s.get('http://httpbin.org/headers')142143* All request hooks have been removed except 'response'.144145* Authentication helpers have been broken out into separate modules. See146 requests-oauthlib_ and requests-kerberos_.147148.. _requests-oauthlib: https://github.com/requests/requests-oauthlib149.. _requests-kerberos: https://github.com/requests/requests-kerberos150151* The parameter for streaming requests was changed from ``prefetch`` to152 ``stream`` and the logic was inverted. In addition, ``stream`` is now153 required for raw response reading.154155 ::156157 # in 0.x, passing prefetch=False would accomplish the same thing158 r = requests.get('https://github.com/timeline.json', stream=True)159 r.raw.read(10)160161* The ``config`` parameter to the requests method has been removed. Some of162 these options are now configured on a ``Session`` such as keep-alive and163 maximum number of redirects. The verbosity option should be handled by164 configuring logging.165166 ::167168 import requests169 import logging170171 # these two lines enable debugging at httplib level (requests->urllib3->httplib)172 # you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.173 # the only thing missing will be the response.body which is not logged.174 import httplib175 httplib.HTTPConnection.debuglevel = 1176 177 logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests178 logging.getLogger().setLevel(logging.DEBUG)179 requests_log = logging.getLogger("requests.packages.urllib3")180 requests_log.setLevel(logging.DEBUG)181 requests_log.propagate = True182 183 requests.get('http://httpbin.org/headers')184185186187Licensing188~~~~~~~~~189190One key difference that has nothing to do with the API is a change in the191license from the ISC_ license to the `Apache 2.0`_ license. The Apache 2.0192license ensures that contributions to requests are also covered by the Apache1932.0 license.194195.. _ISC: http://opensource.org/licenses/ISC196.. _Apache 2.0: http://opensource.org/licenses/Apache-2.0197