Let's persist some cookies across requests::
- s = requests.session()
+ s = requests.Session()
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get("http://httpbin.org/cookies")
Sessions can also be used to provide default data to the request methods. This
is done by providing data to the properties on a session object::
- headers = {'x-test': 'true'}
- auth = ('user', 'pass')
+ s = requests.Session()
+ s.auth = ('user', 'pass')
+ s.headers.update({'x-test': 'true'})
- with requests.session() as c:
- c.auth = auth
- c.headers = headers
-
- # both 'x-test' and 'x-test2' are sent
- c.get('http://httpbin.org/headers', headers={'x-test2': 'true'})
+ # both 'x-test' and 'x-test2' are sent
+ s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})
Any dictionaries that you pass to a request method will be merged with the session-level values that are set. The method-level parameters override session parameters.
``Request`` object you created originally. Here is a simple request to get some
very important information from Wikipedia's servers::
- >>> response = requests.get('http://en.wikipedia.org/wiki/Monty_Python')
+ >>> r = requests.get('http://en.wikipedia.org/wiki/Monty_Python')
If we want to access the headers the server sent back to us, we do this::
- >>> response.headers
+ >>> r.headers
{'content-length': '56170', 'x-content-type-options': 'nosniff', 'x-cache':
'HIT from cp1006.eqiad.wmnet, MISS from cp1010.eqiad.wmnet', 'content-encoding':
'gzip', 'age': '3080', 'content-language': 'en', 'vary': 'Accept-Encoding,Cookie',
However, if we want to get the headers we sent the server, we simply access the
request, and then the request's headers::
- >>> response.request.headers
+ >>> r.request.headers
{'Accept-Encoding': 'identity, deflate, compress, gzip',
'Accept': '*/*', 'User-Agent': 'python-requests/0.13.1'}
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`.
-Configuring Requests
---------------------
-
-Sometimes you may want to configure a request to customize its behavior. To do
-this, you can pass in a ``config`` dictionary to a request or session. See the :ref:`Configuration API Docs <configurations>` to learn more.
-
Keep-Alive
----------
::
from requests.auth import AuthBase
+
class PizzaAuth(AuthBase):
"""Attaches HTTP Pizza Authentication to the given Request object."""
def __init__(self, username):
Make a Request
-------------------
+--------------
Making a request with Requests is very simple.
>>> r = requests.post("http://httpbin.org/post", data=payload)
>>> print r.text
{
- // ...snip... //
+ ...
"form": {
"key2": "value2",
"key1": "value1"
},
- // ...snip... //
+ ...
}
There are many times that you want to send data that is not form-encoded. If you pass in a ``string`` instead of a ``dict``, that data will be posted directly.
>>> r = requests.post(url, files=files)
>>> r.text
{
- // ...snip... //
+ ...
"files": {
"file": "<censored...binary...data>"
},
- // ...snip... //
+ ...
}
You can set the filename explicitly::
>>> r = requests.post(url, files=files)
>>> r.text
{
- // ...snip... //
+ ...
"files": {
"file": "<censored...binary...data>"
},
- // ...snip... //
+ ...
}
If you want, you can send strings to be received as files::
>>> r = requests.post(url, files=files)
>>> r.text
{
- // ...snip... //
+ ...
"files": {
"file": "some,data,to,send\\nanother,row,to,send\\n"
},
- // ...snip... //
+ ...
}