Removed last attempt at documenting how to access Request from Response objects, replaced with section entitled 'Request and Response Objects'
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 fde7ba7 · Browse files · 1 file·+79 added−83 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+79 added−83 removed
- other:+79 added−83 removed
Provenance
- Commit
8b9f1b3- Tree
4491888- Author
Michael Holler <mjholler@noctrl.edu>- Committer
Michael Holler <mjholler@noctrl.edu>- Parent
fde7ba7
1 file changed+79 added−83 removed
modified:docs/user/advanced.rst+79 added-83 removed
43 unmodified lines44 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.45 46 All values that are contained within a session are directly available to you. See the :ref:`Session API Docs <sessionapi>` to learn more.47 48+Request and Response Objects49+----------------------------50 51+Whenever a call is made to requests.*() you are doing two major things. First,52+you are constructing a ``Request`` object which will be sent of to a server53+to request or query some resource. Second, a ``Response`` object is generated54+once ``requests`` gets a response back from the server. The response object 55+contains all of the information returned by the server and also contains the56+``Request`` object you created originally. Here is a simple request to get some57+very important information from Wikipedia's servers::58+ 59+ >>> response = requests.get('http://en.wikipedia.org/wiki/Monty_Python')60+61+If we want to access the headers the server sent back to us, we do this::62+ 63+ >>> response.headers64+ {'content-length': '56170', 'x-content-type-options': 'nosniff', 'x-cache':65+ 'HIT from cp1006.eqiad.wmnet, MISS from cp1010.eqiad.wmnet', 'content-encoding': 66+ 'gzip', 'age': '3080', 'content-language': 'en', 'vary': 'Accept-Encoding,Cookie',67+ 'server': 'Apache', 'last-modified': 'Wed, 13 Jun 2012 01:33:50 GMT',68+ 'connection': 'close', 'cache-control': 'private, s-maxage=0, max-age=0,69+ must-revalidate', 'date': 'Thu, 14 Jun 2012 12:59:39 GMT', 'content-type':70+ 'text/html; charset=UTF-8', 'x-cache-lookup': 'HIT from cp1006.eqiad.wmnet:3128,71+ MISS from cp1010.eqiad.wmnet:80'}72+73+However, if we want to get the headers we sent the server, we simply access the74+request, and then the request's headers::75+76+ >>> response.request.headers77+ {'Accept-Encoding': 'identity, deflate, compress, gzip',78+ 'Accept': '*/*', 'User-Agent': 'python-requests/0.13.1'}79+80+Requests have these attributes:81+82+ * Variable Attributes83+ * allow_redirects84+ * auth85+ * cert86+ * config87+ * cookies88+ * data89+ * files90+ * headers91+ * hooks92+ * method93+ * params94+ * prefetch95+ * proxies96+ * redirect97+ * response98+ * sent99+ * session100+ * timeout101+ * url102+ * Class Methods103+ * deregister_hook104+ * full_url105+ * path_url106+ * register_hook107+ * send108+109+Responses have the following attributes:110+111+ * Variable Attributes112+ * config113+ * cookies114+ * encoding115+ * error116+ * headers117+ * history118+ * raw119+ * request120+ * status_code121+ * url122+ * Class Methods123+ * content124+ * json125+ * raise_for_status126+ * text127+128 SSL Cert Verification129 ---------------------130 131 Requests 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::170 unmodified lines302 303 >>> requests.get('http://pizzabin.org/admin', auth=PizzaAuth('kenneth'))304 <Response [200]>305 227-Accessing Request Information228------------------------------229-230-Every request has two main parts, the ``request`` and the ``response``. It's231-probably obvious why you'd want to access the response, but there might also be232-times where accessing the request's data members might prove useful, especially233-when creating a custom authentication implementation. In this case, as in234-others, you may want to access or change a part of the request.235-236-Consider a situation where you create make a request in one method, but use the237-``Requests`` object returned by your request in another.238-239-::240-241- def mystery_request():242- return requests.get('http://en.wikipedia.org/wiki/Monty_Python')243-244- def serious_code():245- r = mystery_request()246-247-Now, we have this object ``r``, but how do we tell what's in it? We can't do248-much until we know what kind of request returned this content, so let's figure249-that out::250-251- >>> r.request.method252- 'GET'253-254-Every ``Requests`` object contains the full request object. Now that we know255-it's a ``GET``, we might want to know what headers we sent with it::256-257- >>> r.request.headers258- {'Accept-Encoding': 'identity, deflate, compress, gzip',259- 'Accept': '*/*', 'User-Agent': 'python-requests/0.13.1'}260-261-We can also do this for POSTs, and any other request for that matter::262-263- >>> r = requests.post('http://api.somedomain.org', data=some_data, 264- headers=some_headers)265- >>> r.request.data == some_data266- True267-268-Request Object Attributes269--------------------------270-271-``Request`` objects have the following attributes:272-273- * allow_redirects274- * True enables full redirects275- * auth276- * Basic authentication tuple or object277- * cert278- * SSL certificate279- * config280- * Dictionary of configurations for the request281- * data282- * Dictionary, bytes or file stream to attach to request body283- * files284- * Dictionary of files for multipart upload285- * headers286- * Dictionary of HTTP headers sent with request287- * hooks288- * Event handling hooks289- * method290- * All CAPs string for request method type (e.x. 'GET')291- * params292- * Dictionary or bytes of query to attach to the end of the url293- * prefetch294- * Setting this to True downloads the reponse's content body as soon as295- the request is made296- * proxies297- * Dictionary of protocol to proxy298- * redirect299- * True if request is part of a redirect chain300- * sent301- * True if the request has been sent302- * session303- * timeout304- * Float representing length of time to wait for response305- * url306- * verify307- * Set to true if you want to ``requests`` to verify ``url``'s SSL308- certificate309-306 Streaming Requests307 ------------------308 309 With ``requests.Response.iter_lines()`` you can easily iterate over streaming222 unmodified lines