| OLD | NEW |
| (Empty) |
| 1 .. _api: | |
| 2 | |
| 3 Developer Interface | |
| 4 =================== | |
| 5 | |
| 6 .. module:: requests | |
| 7 | |
| 8 This part of the documentation covers all the interfaces of Requests. For | |
| 9 parts where Requests depends on external libraries, we document the most | |
| 10 important right here and provide links to the canonical documentation. | |
| 11 | |
| 12 | |
| 13 Main Interface | |
| 14 -------------- | |
| 15 | |
| 16 All of Requests' functionality can be accessed by these 7 methods. | |
| 17 They all return an instance of the :class:`Response <Response>` object. | |
| 18 | |
| 19 .. autofunction:: request | |
| 20 | |
| 21 .. autofunction:: head | |
| 22 .. autofunction:: get | |
| 23 .. autofunction:: post | |
| 24 .. autofunction:: put | |
| 25 .. autofunction:: patch | |
| 26 .. autofunction:: delete | |
| 27 | |
| 28 Exceptions | |
| 29 ---------- | |
| 30 | |
| 31 .. autoexception:: requests.RequestException | |
| 32 .. autoexception:: requests.ConnectionError | |
| 33 .. autoexception:: requests.HTTPError | |
| 34 .. autoexception:: requests.URLRequired | |
| 35 .. autoexception:: requests.TooManyRedirects | |
| 36 .. autoexception:: requests.ConnectTimeout | |
| 37 .. autoexception:: requests.ReadTimeout | |
| 38 .. autoexception:: requests.Timeout | |
| 39 | |
| 40 | |
| 41 Request Sessions | |
| 42 ---------------- | |
| 43 | |
| 44 .. _sessionapi: | |
| 45 | |
| 46 .. autoclass:: Session | |
| 47 :inherited-members: | |
| 48 | |
| 49 | |
| 50 Lower-Level Classes | |
| 51 ------------------- | |
| 52 | |
| 53 .. autoclass:: requests.Request | |
| 54 :inherited-members: | |
| 55 | |
| 56 .. autoclass:: Response | |
| 57 :inherited-members: | |
| 58 | |
| 59 | |
| 60 Lower-Lower-Level Classes | |
| 61 ------------------------- | |
| 62 | |
| 63 .. autoclass:: requests.PreparedRequest | |
| 64 :inherited-members: | |
| 65 | |
| 66 .. autoclass:: requests.adapters.HTTPAdapter | |
| 67 :inherited-members: | |
| 68 | |
| 69 Authentication | |
| 70 -------------- | |
| 71 | |
| 72 .. autoclass:: requests.auth.AuthBase | |
| 73 .. autoclass:: requests.auth.HTTPBasicAuth | |
| 74 .. autoclass:: requests.auth.HTTPProxyAuth | |
| 75 .. autoclass:: requests.auth.HTTPDigestAuth | |
| 76 | |
| 77 | |
| 78 | |
| 79 Encodings | |
| 80 --------- | |
| 81 | |
| 82 .. autofunction:: requests.utils.get_encodings_from_content | |
| 83 .. autofunction:: requests.utils.get_encoding_from_headers | |
| 84 .. autofunction:: requests.utils.get_unicode_from_response | |
| 85 | |
| 86 | |
| 87 .. _api-cookies: | |
| 88 | |
| 89 Cookies | |
| 90 ------- | |
| 91 | |
| 92 .. autofunction:: requests.utils.dict_from_cookiejar | |
| 93 .. autofunction:: requests.utils.cookiejar_from_dict | |
| 94 .. autofunction:: requests.utils.add_dict_to_cookiejar | |
| 95 | |
| 96 .. autoclass:: requests.cookies.RequestsCookieJar | |
| 97 :inherited-members: | |
| 98 | |
| 99 .. autoclass:: requests.cookies.CookieConflictError | |
| 100 :inherited-members: | |
| 101 | |
| 102 | |
| 103 | |
| 104 Status Code Lookup | |
| 105 ------------------ | |
| 106 | |
| 107 .. autoclass:: requests.codes | |
| 108 | |
| 109 :: | |
| 110 | |
| 111 >>> requests.codes['temporary_redirect'] | |
| 112 307 | |
| 113 | |
| 114 >>> requests.codes.teapot | |
| 115 418 | |
| 116 | |
| 117 >>> requests.codes['\o/'] | |
| 118 200 | |
| 119 | |
| 120 | |
| 121 | |
| 122 Migrating to 1.x | |
| 123 ---------------- | |
| 124 | |
| 125 This section details the main differences between 0.x and 1.x and is meant | |
| 126 to ease the pain of upgrading. | |
| 127 | |
| 128 | |
| 129 API Changes | |
| 130 ~~~~~~~~~~~ | |
| 131 | |
| 132 * ``Response.json`` is now a callable and not a property of a response. | |
| 133 | |
| 134 :: | |
| 135 | |
| 136 import requests | |
| 137 r = requests.get('https://github.com/timeline.json') | |
| 138 r.json() # This *call* raises an exception if JSON decoding fails | |
| 139 | |
| 140 * The ``Session`` API has changed. Sessions objects no longer take parameters. | |
| 141 ``Session`` is also now capitalized, but it can still be | |
| 142 instantiated with a lowercase ``session`` for backwards compatibility. | |
| 143 | |
| 144 :: | |
| 145 | |
| 146 s = requests.Session() # formerly, session took parameters | |
| 147 s.auth = auth | |
| 148 s.headers.update(headers) | |
| 149 r = s.get('http://httpbin.org/headers') | |
| 150 | |
| 151 * All request hooks have been removed except 'response'. | |
| 152 | |
| 153 * Authentication helpers have been broken out into separate modules. See | |
| 154 requests-oauthlib_ and requests-kerberos_. | |
| 155 | |
| 156 .. _requests-oauthlib: https://github.com/requests/requests-oauthlib | |
| 157 .. _requests-kerberos: https://github.com/requests/requests-kerberos | |
| 158 | |
| 159 * The parameter for streaming requests was changed from ``prefetch`` to | |
| 160 ``stream`` and the logic was inverted. In addition, ``stream`` is now | |
| 161 required for raw response reading. | |
| 162 | |
| 163 :: | |
| 164 | |
| 165 # in 0.x, passing prefetch=False would accomplish the same thing | |
| 166 r = requests.get('https://github.com/timeline.json', stream=True) | |
| 167 for chunk in r.iter_content(8192): | |
| 168 ... | |
| 169 | |
| 170 * The ``config`` parameter to the requests method has been removed. Some of | |
| 171 these options are now configured on a ``Session`` such as keep-alive and | |
| 172 maximum number of redirects. The verbosity option should be handled by | |
| 173 configuring logging. | |
| 174 | |
| 175 :: | |
| 176 | |
| 177 import requests | |
| 178 import logging | |
| 179 | |
| 180 # Enabling debugging at http.client level (requests->urllib3->http.client) | |
| 181 # you will see the REQUEST, including HEADERS and DATA, and RESPONSE with
HEADERS but without DATA. | |
| 182 # the only thing missing will be the response.body which is not logged. | |
| 183 try: # for Python 3 | |
| 184 from http.client import HTTPConnection | |
| 185 except ImportError: | |
| 186 from httplib import HTTPConnection | |
| 187 HTTPConnection.debuglevel = 1 | |
| 188 | |
| 189 logging.basicConfig() # you need to initialize logging, otherwise you will
not see anything from requests | |
| 190 logging.getLogger().setLevel(logging.DEBUG) | |
| 191 requests_log = logging.getLogger("requests.packages.urllib3") | |
| 192 requests_log.setLevel(logging.DEBUG) | |
| 193 requests_log.propagate = True | |
| 194 | |
| 195 requests.get('http://httpbin.org/headers') | |
| 196 | |
| 197 | |
| 198 | |
| 199 Licensing | |
| 200 ~~~~~~~~~ | |
| 201 | |
| 202 One key difference that has nothing to do with the API is a change in the | |
| 203 license from the ISC_ license to the `Apache 2.0`_ license. The Apache 2.0 | |
| 204 license ensures that contributions to Requests are also covered by the Apache | |
| 205 2.0 license. | |
| 206 | |
| 207 .. _ISC: http://opensource.org/licenses/ISC | |
| 208 .. _Apache 2.0: http://opensource.org/licenses/Apache-2.0 | |
| 209 | |
| 210 | |
| 211 Migrating to 2.x | |
| 212 ---------------- | |
| 213 | |
| 214 | |
| 215 Compared with the 1.0 release, there were relatively few backwards | |
| 216 incompatible changes, but there are still a few issues to be aware of with | |
| 217 this major release. | |
| 218 | |
| 219 For more details on the changes in this release including new APIs, links | |
| 220 to the relevant GitHub issues and some of the bug fixes, read Cory's blog_ | |
| 221 on the subject. | |
| 222 | |
| 223 .. _blog: http://lukasa.co.uk/2013/09/Requests_20/ | |
| 224 | |
| 225 | |
| 226 API Changes | |
| 227 ~~~~~~~~~~~ | |
| 228 | |
| 229 * There were a couple changes to how Requests handles exceptions. | |
| 230 ``RequestException`` is now a subclass of ``IOError`` rather than | |
| 231 ``RuntimeError`` as that more accurately categorizes the type of error. | |
| 232 In addition, an invalid URL escape sequence now raises a subclass of | |
| 233 ``RequestException`` rather than a ``ValueError``. | |
| 234 | |
| 235 :: | |
| 236 | |
| 237 requests.get('http://%zz/') # raises requests.exceptions.InvalidURL | |
| 238 | |
| 239 Lastly, ``httplib.IncompleteRead`` exceptions caused by incorrect chunked | |
| 240 encoding will now raise a Requests ``ChunkedEncodingError`` instead. | |
| 241 | |
| 242 * The proxy API has changed slightly. The scheme for a proxy URL is now | |
| 243 required. | |
| 244 | |
| 245 :: | |
| 246 | |
| 247 proxies = { | |
| 248 "http": "10.10.1.10:3128", # use http://10.10.1.10:3128 instead | |
| 249 } | |
| 250 | |
| 251 # In requests 1.x, this was legal, in requests 2.x, | |
| 252 # this raises requests.exceptions.MissingSchema | |
| 253 requests.get("http://example.org", proxies=proxies) | |
| 254 | |
| 255 | |
| 256 Behavioural Changes | |
| 257 ~~~~~~~~~~~~~~~~~~~~~~~ | |
| 258 | |
| 259 * Keys in the ``headers`` dictionary are now native strings on all Python | |
| 260 versions, i.e. bytestrings on Python 2 and unicode on Python 3. If the | |
| 261 keys are not native strings (unicode on Python2 or bytestrings on Python 3) | |
| 262 they will be converted to the native string type assuming UTF-8 encoding. | |
| 263 | |
| OLD | NEW |