Skip to content
Myr

update docs

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

authored by Kenneth Reitz · committed by Kenneth Reitz · parent 0665129 · Browse files · 2 files+19 added−27 removed
This commit

State

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

Diff stats

2 files+19 added−27 removed

  • other:+19 added−27 removed

2 files+19 added−27 removed

modified:docs/user/advanced.rst+10 added-18 removed
15 unmodified lines
16A session object has all the methods of the main Requests API.1718Let's persist some cookies across requests::1920 s = requests.session()20 s = requests.Session()2122 s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')23 r = s.get("http://httpbin.org/cookies")24
3 unmodified lines
2829Sessions can also be used to provide default data to the request methods. This30is done by providing data to the properties on a session object::3132 headers = {'x-test': 'true'}33 auth = ('user', 'pass')32 s = requests.Session()33 s.auth = ('user', 'pass')34 s.headers.update({'x-test': 'true'})3535 with requests.session() as c:36 c.auth = auth37 c.headers = headers36 # both 'x-test' and 'x-test2' are sent37 s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})3839 # both 'x-test' and 'x-test2' are sent40 c.get('http://httpbin.org/headers', headers={'x-test2': 'true'})394240Any 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.4142.. admonition:: Remove a Value From a Dict Parameter43
11 unmodified lines
55contains all of the information returned by the server and also contains the56``Request`` object you created originally. Here is a simple request to get some57very important information from Wikipedia's servers::5862 >>> response = requests.get('http://en.wikipedia.org/wiki/Monty_Python')59 >>> r = requests.get('http://en.wikipedia.org/wiki/Monty_Python')6061If we want to access the headers the server sent back to us, we do this::6266 >>> response.headers63 >>> r.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',
4 unmodified lines
7273However, if we want to get the headers we sent the server, we simply access the74request, and then the request's headers::7579 >>> response.request.headers76 >>> r.request.headers77 {'Accept-Encoding': 'identity, deflate, compress, gzip',78 'Accept': '*/*', 'User-Agent': 'python-requests/0.13.1'}7980
47 unmodified lines
128 ...129130You 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`.131135Configuring Requests136--------------------132138Sometimes you may want to configure a request to customize its behavior. To do139this, you can pass in a ``config`` dictionary to a request or session. See the :ref:`Configuration API Docs <configurations>` to learn more.140141133Keep-Alive134----------135136Excellent news — thanks to urllib3, keep-alive is 100% automatic within a session! Any requests that you make within a session will automatically reuse the appropriate connection!
57 unmodified lines
194195::196197 from requests.auth import AuthBase198199 class PizzaAuth(AuthBase):200 """Attaches HTTP Pizza Authentication to the given Request object."""201 def __init__(self, username):202 # setup any auth-related data here
286 unmodified lines
modified:docs/user/quickstart.rst+9 added-9 removed
17 unmodified lines
18Let's get started with some simple examples.192021Make a Request22------------------22--------------2324Making a request with Requests is very simple.2526Begin by importing the Requests module::
147 unmodified lines
174 >>> payload = {'key1': 'value1', 'key2': 'value2'}175 >>> r = requests.post("http://httpbin.org/post", data=payload)176 >>> print r.text177 {178 // ...snip... //178 ...179 "form": {180 "key2": "value2",181 "key1": "value1"182 },183 // ...snip... //183 ...184 }185186There 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.187
16 unmodified lines
204205 >>> r = requests.post(url, files=files)206 >>> r.text207 {208 // ...snip... //208 ...209 "files": {210 "file": "<censored...binary...data>"211 },212 // ...snip... //212 ...213 }214215You can set the filename explicitly::216
2 unmodified lines
219220 >>> r = requests.post(url, files=files)221 >>> r.text222 {223 // ...snip... //223 ...224 "files": {225 "file": "<censored...binary...data>"226 },227 // ...snip... //227 ...228 }229230If you want, you can send strings to be received as files::231
2 unmodified lines
234235 >>> r = requests.post(url, files=files)236 >>> r.text237 {238 // ...snip... //238 ...239 "files": {240 "file": "some,data,to,send\\nanother,row,to,send\\n"241 },242 // ...snip... //242 ...243 }244245246Response Status Codes
175 unmodified lines