api.rst
This file
Path
docs/api.rst
177 lines · 4.0 KiB
Last changed by
Merge pull request #1231 from davidfischer/docs-migration1.0Kenneth Reitzbefore adoption08cab48
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:434445Exceptions46~~~~~~~~~~4748.. module:: requests4950.. autoexception:: RequestException51.. autoexception:: ConnectionError52.. autoexception:: HTTPError53.. autoexception:: URLRequired54.. autoexception:: TooManyRedirects555657Status Code Lookup58~~~~~~~~~~~~~~~~~~5960.. autofunction:: requests.codes6162::6364 >>> requests.codes['temporary_redirect']65 3076667 >>> requests.codes.teapot68 4186970 >>> requests.codes['\o/']71 2007273Cookies74~~~~~~~7576.. autofunction:: dict_from_cookiejar77.. autofunction:: cookiejar_from_dict78.. autofunction:: add_dict_to_cookiejar798081Encodings82~~~~~~~~~8384.. autofunction:: get_encodings_from_content85.. autofunction:: get_encoding_from_headers86.. autofunction:: get_unicode_from_response87.. autofunction:: decode_gzip888990Classes91~~~~~~~9293.. autoclass:: requests.Response94 :inherited-members:9596.. autoclass:: requests.Request97 :inherited-members:9899.. autoclass:: requests.PreparedRequest100 :inherited-members:101102.. _sessionapi:103104.. autoclass:: requests.Session105 :inherited-members:106107108Migrating to 1.x109----------------110111This section details the main differences between 0.x and 1.x and is meant112to ease the pain of upgrading.113114115API Changes116~~~~~~~~~~~117118* ``Response.json`` is now a callable and not a property of a response.119120 ::121122 import requests123 r = requests.get('https://github.com/timeline.json')124 r.json() # This *call* raises an exception if JSON decoding fails125126* The ``Session`` API has changed. Sessions objects no longer take parameters.127 ``Session`` is also now capitalized, but it can still be128 instantiated with a lowercase ``session`` for backwards compatibility.129130 ::131132 s = requests.Session() # formerly, session took parameters133 s.auth = auth134 s.headers.update(headers)135 r = s.get('http://httpbin.org/headers')136137* All request hooks have been removed except 'response'.138139* Authentication helpers have been broken out into separate modules. See140 requests-oauthlib_ and requests-kerberos_.141142.. _requests-oauthlib: https://github.com/requests/requests-oauthlib143.. _requests-kerberos: https://github.com/requests/requests-kerberos144145* The parameter for streaming requests was changed from ``prefetch`` to146 ``stream`` and the logic was inverted. In addition, ``stream`` is now147 required for raw response reading.148149 ::150151 # in 0.x, passing prefetch=False would accomplish the same thing152 r = requests.get('https://github.com/timeline.json', stream=True)153 r.raw.read(10)154155* The ``config`` parameter to the requests method has been removed. Some of156 these options are now configured on a ``Session`` such as keep-alive and157 maximum number of redirects. The verbosity option should be handled by158 configuring logging.159160 ::161162 # Verbosity should now be configured with logging163 my_config = {'verbose': sys.stderr}164 requests.get('http://httpbin.org/headers', config=my_config) # bad!165166167Licensing168~~~~~~~~~169170One key difference that has nothing to do with the API is a change in the171license from the ISC_ license to the `Apache 2.0`_ license. The Apache 2.0172license ensures that contributions to requests are also covered by the Apache1732.0 license.174175.. _ISC: http://opensource.org/licenses/ISC176.. _Apache 2.0: http://opensource.org/licenses/Apache-2.0177