Skip to content
Myr

Merge pull request #2139 from dpursehouse/line-wrapping

Before adoption: this commit came in with the history the project adopted, so no landing records it.

More line wrapping

authored by Cory Benfield · committed by Cory Benfield · parents 587a515, 44e426e · Browse files · 2 files+38 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+38 added−14 removed

  • other:+38 added−14 removed

2 files+38 added−14 removed

modified:docs/user/advanced.rst+36 added-13 removed
35 unmodified lines
36 # both 'x-test' and 'x-test2' are sent37 s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})383940Any dictionaries that you pass to a request method will be merged with the session-level values that are set. The method-level parameters override session parameters.40Any dictionaries that you pass to a request method will be merged with the41session-level values that are set. The method-level parameters override session42parameters.4344.. admonition:: Remove a Value From a Dict Parameter4544 Sometimes you'll want to omit session-level keys from a dict parameter. To do this, you simply set that key's value to ``None`` in the method-level parameter. It will automatically be omitted.46 Sometimes you'll want to omit session-level keys from a dict parameter. To47 do this, you simply set that key's value to ``None`` in the method-level48 parameter. It will automatically be omitted.4946All values that are contained within a session are directly available to you. See the :ref:`Session API Docs <sessionapi>` to learn more.50All values that are contained within a session are directly available to you.51See the :ref:`Session API Docs <sessionapi>` to learn more.5253Request and Response Objects54----------------------------55
98 unmodified lines
154155SSL Cert Verification156---------------------157153Requests can verify SSL certificates for HTTPS requests, just like a web browser. To check a host's SSL certificate, you can use the ``verify`` argument::158Requests can verify SSL certificates for HTTPS requests, just like a web browser.159To check a host's SSL certificate, you can use the ``verify`` argument::160161 >>> requests.get('https://kennethreitz.com', verify=True)162 requests.exceptions.SSLError: hostname 'kennethreitz.com' doesn't match either of '*.herokuapp.com', 'herokuapp.com'163
12 unmodified lines
176 <Response [200]>177178By default, ``verify`` is set to True. Option ``verify`` only applies to host certs.179174You can also specify a local cert to use as client side certificate, as a single file (containing the private key and the certificate) or as a tuple of both file's path::180You can also specify a local cert to use as client side certificate, as a single181file (containing the private key and the certificate) or as a tuple of both182file's path::183184 >>> requests.get('https://kennethreitz.com', cert=('/path/server.crt', '/path/key'))185 <Response [200]>186
13 unmodified lines
200201 tarball_url = 'https://github.com/kennethreitz/requests/tarball/master'202 r = requests.get(tarball_url, stream=True)203196At this point only the response headers have been downloaded and the connection remains open, hence allowing us to make content retrieval conditional::204At this point only the response headers have been downloaded and the connection205remains open, hence allowing us to make content retrieval conditional::206207 if int(r.headers['content-length']) < TOO_LONG:208 content = r.content209 ...210202You can further control the workflow by use of the :class:`Response.iter_content <requests.Response.iter_content>` and :class:`Response.iter_lines <requests.Response.iter_lines>` methods. Alternatively, you can read the undecoded body from the underlying urllib3 :class:`urllib3.HTTPResponse <urllib3.response.HTTPResponse>` at :class:`Response.raw <requests.Response.raw>`.211You can further control the workflow by use of the :class:`Response.iter_content <requests.Response.iter_content>`212and :class:`Response.iter_lines <requests.Response.iter_lines>` methods.213Alternatively, you can read the undecoded body from the underlying214urllib3 :class:`urllib3.HTTPResponse <urllib3.response.HTTPResponse>` at215:class:`Response.raw <requests.Response.raw>`.216217If you set ``stream`` to ``True`` when making a request, Requests cannot218release the connection back to the pool unless you consume all the data or call219:class:`Response.close <requests.Response.close>`. This can lead to
11 unmodified lines
231232Keep-Alive233----------234222Excellent news — thanks to urllib3, keep-alive is 100% automatic within a session! Any requests that you make within a session will automatically reuse the appropriate connection!235Excellent news — thanks to urllib3, keep-alive is 100% automatic within a session!236Any requests that you make within a session will automatically reuse the appropriate237connection!238224Note that connections are only released back to the pool for reuse once all body data has been read; be sure to either set ``stream`` to ``False`` or read the ``content`` property of the ``Response`` object.239Note that connections are only released back to the pool for reuse once all body240data has been read; be sure to either set ``stream`` to ``False`` or read the241``content`` property of the ``Response`` object.242243244Streaming Uploads245-----------------246230Requests supports streaming uploads, which allow you to send large streams or files without reading them into memory. To stream and upload, simply provide a file-like object for your body::247Requests supports streaming uploads, which allow you to send large streams or248files without reading them into memory. To stream and upload, simply provide a249file-like object for your body::250251 with open('massive-body', 'rb') as f:252 requests.post('http://some.url/streamed', data=f)253254255Chunk-Encoded Requests256----------------------257239Requests also supports Chunked transfer encoding for outgoing and incoming requests. To send a chunk-encoded request, simply provide a generator (or any iterator without a length) for your body::258Requests also supports Chunked transfer encoding for outgoing and incoming requests.259To send a chunk-encoded request, simply provide a generator (or any iterator without260a length) for your body::261262263 def gen():264 yield 'hi'
334 unmodified lines
599600Link Headers601------------602582Many HTTP APIs feature Link headers. They make APIs more self describing and discoverable.603Many HTTP APIs feature Link headers. They make APIs more self describing and604discoverable.605584GitHub uses these for `pagination <http://developer.github.com/v3/#pagination>`_ in their API, for example::606GitHub uses these for `pagination <http://developer.github.com/v3/#pagination>`_607in their API, for example::608609 >>> url = 'https://api.github.com/users/kennethreitz/repos?page=1&per_page=10'610 >>> r = requests.head(url=url)611 >>> r.headers['link']
94 unmodified lines
modified:docs/user/quickstart.rst+2 added-1 removed
202 unmodified lines
203 },204 ...205 }206207There are many times that you want to send data that is not form-encoded. If you pass in a ``string`` instead of a ``dict``, that data will be posted directly.207There are many times that you want to send data that is not form-encoded. If208you pass in a ``string`` instead of a ``dict``, that data will be posted directly.209210For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data::211212 >>> import json
233 unmodified lines