Skip to content
Myr

Added documentation on how to access and use the Request object.

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

authored by Michael Holler · committed by Michael Holler · parent 1d19313 · Browse files · 1 file+65 added−0 removed
This commit

State

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

Diff stats

1 file+65 added−0 removed

  • other:+65 added−0 removed

1 file+65 added−0 removed

modified:docs/user/advanced.rst+65 added
222 unmodified lines
223224 >>> requests.get('http://pizzabin.org/admin', auth=PizzaAuth('kenneth'))225 <Response [200]>226227Accessing Request Information228-----------------------------229230Every request has two main parts, the ``request`` and the ``response``. It's231probably obvious why you'd want to access the response, but there might also be232times where accessing the request's data members might prove useful, especially233when creating a custom authentication implementation. In this case, as in234others, you may want to access or change a part of the request.235236Consider a situation where you create make a request in one method, but use the237``Requests`` object returned by your request in another.238239::240241 def mystery_request():242 return requests.get('http://en.wikipedia.org/wiki/Monty_Python')243244 def serious_code():245 r = mystery_request()246247Now, we have this object ``r``, but how do we tell what's in it? We can't do248much until we know what kind of request returned this content, so let's figure249that out::250251 >>> r.request.method252 'GET'253254Every ``Requests`` object contains the full request object. Now that we know255it's a ``GET``, we might want to know what headers we sent with it::256257 >>> r.request.headers258 {'Accept-Encoding': 'identity, deflate, compress, gzip',259 'Accept': '*/*', 'User-Agent': 'python-requests/0.13.1'}260261We can also do this for POSTs, and any other request for that matter::262263 >>> r = requests.post('http://api.somedomain.org', data=some_data, 264 headers=some_headers)265 >>> r.request.data == some_data266 True267268``Request`` objects have the following attributes:269270 * allow_redirects271 * auth272 * cert273 * config274 * data275 * files276 * headers277 * hooks278 * method279 * params280 * prefetch281 * proxies282 * redirect283 * sent284 * session285 * timeout286 * url287 * verify288289 290 291292293Streaming Requests294------------------295296With ``requests.Response.iter_lines()`` you can easily iterate over streaming
222 unmodified lines