From 991b853bd948dd90353a77807563e8ad32b113ab Mon Sep 17 00:00:00 2001 From: Harry Waye Date: Thu, 30 Aug 2012 18:14:37 +0100 Subject: [PATCH] Updated content workflow section of the advanced use docs --- docs/user/advanced.rst | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/docs/user/advanced.rst b/docs/user/advanced.rst index 3b665e0..d976503 100644 --- a/docs/user/advanced.rst +++ b/docs/user/advanced.rst @@ -116,26 +116,20 @@ If you specify a wrong path or an invalid cert:: Body Content Workflow --------------------- -By default, when you make a request, the body of the response isn't downloaded immediately. The response headers are downloaded when you make a request, but the content isn't downloaded until you access the :class:`Response.content` attribute. - -Let's walk through it:: +By default, when you make a request, the body of the response is downloaded immediately. You can override this behavior and defer downloading the response body until you access the :class:`Response.content` attribute with the ``prefetch`` parameter:: tarball_url = 'https://github.com/kennethreitz/requests/tarball/master' - r = requests.get(tarball_url) - -The request has been made, but the connection is still open. The response body has not been downloaded yet. - -:: - - r.content + r = requests.get(tarball_url, prefetch=False) -The content has been downloaded and cached. +At this point only the response headers have been downloaded and the connection remains open, hence allowing us to make content retrieval conditional:: -You can override this default behavior with the ``prefetch`` parameter:: + if int(r.headers['content-length']) < TOO_LONG: + content = r.content + ... - r = requests.get(tarball_url, prefetch=True) - # Blocks until all of request body has been downloaded. +You can further control the workflow by use of the :class:`Response.iter_content` and :class:`Response.iter_lines` methods, or reading from the underlying urllib3 :class:`urllib3.HTTPResponse` at :class:`Response.raw`. +Note that in versions prior to 0.13.6 the ``prefetch`` default was set to ``False``. Configuring Requests -------------------- -- 2.7.4