Skip to content
Myr

Add PreparedRequest recipe to the docs

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

authored by Ian Cordasco · committed by Ian Cordasco · parent 1abd137 · Browse files · 1 file+35 added−1 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+35 added−1 removed

  • other:+35 added−1 removed

1 file+35 added−1 removed

modified:docs/user/advanced.rst+35 added-1 removed
73 unmodified lines
74request, and then the request's headers::7576 >>> r.request.headers77 {'Accept-Encoding': 'identity, deflate, compress, gzip',78 'Accept': '*/*', 'User-Agent': 'python-requests/0.13.1'}78 'Accept': '*/*', 'User-Agent': 'python-requests/1.2.0'}7980Prepared Requests81-----------------8283Whenever you receive a :class:`Response <requests.models.Response>` object 84from an API call or a Session call, the ``request`` attribute is actually the 85``PreparedRequest`` that was used. In some cases you may wish to do some extra 86work to the body or headers (or anything else really) before sending a 87request. The simple recipe for this is the following::8889 from requests import Request, Session9091 s = Session()92 prepped = Request('GET', # or any other method, 'POST', 'PUT', etc.93 url,94 data=data95 headers=headers96 # ...97 ).prepare()98 # do something with prepped.body99 # do something with prepped.headers100 resp = s.send(prepped,101 stream=stream,102 verify=verify,103 proxies=proxies,104 cert=cert,105 timeout=timeout,106 # etc.107 )108 print(resp.status_code)109110Since you are not doing anything special with the ``Request`` object, you 111prepare it immediately and modified the ``PreparedRequest`` object. You then 112send that with the other parameters you would have sent to ``requests.*`` or 113``Sesssion.*``. 114115SSL Cert Verification116---------------------117118Requests 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::
421 unmodified lines