Body Content Workflow

By default, when you make a request, the body of the response is downloaded immediately. You can override this behaviour and defer downloading the response body until you access the Response.content attribute with the stream parameter:

  1. tarball_url = 'https://github.com/kennethreitz/requests/tarball/master'
  2. r = requests.get(tarball_url, stream=True)

At this point only the response headers have been downloaded and the connection remains open, hence allowing us to make content retrieval conditional:

  1. if int(r.headers['content-length']) < TOO_LONG:
  2. content = r.content
  3. ...

You can further control the workflow by use of the Response.iter_content and Response.iter_lines methods. Alternatively, you can read the undecoded body from the underlying urllib3 urllib3.HTTPResponse at Response.raw.

If you set stream to True when making a request, Requests cannot release the connection back to the pool unless you consume all the data or call Response.close. This can lead to inefficiency with connections. If you find yourself partially reading request bodies (or not reading them at all) while using stream=True, you should consider using contextlib.closing (documented here), like this:

  1. from contextlib import closing
  2. with closing(requests.get('http://httpbin.org/get', stream=True)) as r:
  3. # Do things with the response here.