big quick start updates
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 52b9f01 · Browse files · 1 file·+72 added−16 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+72 added−16 removed
- other:+72 added−16 removed
Provenance
- Commit
60558fd- Tree
9e7cb23- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
52b9f01
1 file changed+72 added−16 removed
modified:docs/user/quickstart.rst+72 added-16 removed
37 unmodified lines38 39 >>> r.content40 '[{"repository":{"open_issues":0,"url":"https://github.com/...41 42+Requests does its best to decode content from the server. Most unicode charsets, ``gzip``, and ``deflate`` encodings are all seamlessly decoded.43 44+45 Make a POST Request44-------------------46+-------------------47 46-POST requests are equally simple ::48+POST requests are equally simple::49 48- >>> r = requests.post("http://httpbin.org/post")49- 50+ r = requests.post("http://httpbin.org/post")51 51-Suppose you want to send data over HTTP. Simply pass a data52-argument to the requests.post method with your dictionary ::52 54- >>> dataDict = {"key1":"value1", "key2":"value2"}55- >>> r = requests.post("http://httpbin.org/post", data=dataDict)53+Typically, you want to send some form-encoded data — much like an HTML form.54+To do this, simply pass a dictionary to the `data` argument. Your dictionary of data will automatically be form-encoded when the request is made::55+56+ >>> payload = {'key1': 'value1', 'key2': 'value2'}57+ >>> r = requests.post("http://httpbin.org/post", data=payload)58+ >>> print r.content59+ {60+ "origin": "179.13.100.4",61+ "files": {},62+ "form": {63+ "key2": "value2",64+ "key1": "value1"65+ },66+ "url": "http://httpbin.org/post",67+ "args": {},68+ "headers": {69+ "Content-Length": "23",70+ "Accept-Encoding": "identity, deflate, compress, gzip",71+ "Accept": "*/*",72+ "User-Agent": "python-requests/0.8.0",73+ "Host": "127.0.0.1:7077",74+ "Content-Type": "application/x-www-form-urlencoded"75+ },76+ "data": ""77+ }78+79+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.80+81+For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data::82+83+ url = 'https://api.github.com/some/endpoint'84+ payload = {'some': 'data'}85+86+ r = requests.post(url, data=json.dumps(payload))87+88+89+Custom Headers90+--------------91+92+If you'd like to add HTTP headers to a request, simply pass in a ``dict`` to the93+``headers`` parameter.94+95+For example, we didn't specify our content-type in the previous example::96+97+ url = 'https://api.github.com/some/endpoint'98+ payload = {'some': 'data'}99+ headers = {'content-type': 'application/json'}100+101+ r = requests.post(url, data=json.dumps(payload), headers=headers)102+103+104+POST a Multipart-Encoded File105+-----------------------------106+107+Requests makes it simple to upload Multipart-encoded files::108+109+ >>> url = 'http://httpbin.org/post'110+ >>> files = {'report.xls': open('report.xls', 'rb')}111+112+ >>> r = requests.post(url, data=None, files=files)113 >>> r.content57- '{\n "origin": "::ffff:YourIpAddress", \n "files": {}, \n "form": {\n "key2": "value2", \n "key1": "value1"\n }, ...114 59-Note the data= argument is equivalent to -d in cURL scripts. dataDict will60-be form-encoded. 115 116+117+118+119+120 Response Status Codes121 ---------------------122 123 We can check the response status code::6 unmodified lines130 131 >>> r.status_code == requests.codes.ok132 True133 76-If we made a bad request, we can raise it with134+If we made a bad request (non-200 response), we can raise it with135 :class:`Response.raise_for_status()`::136 137 >>> _r = requests.get('http://httpbin.org/status/404')138 >>> _r.status_code76 unmodified lines215 216 Most web services require authentication. There many different types of217 authentication, but the most common is called HTTP Basic Auth.218 161-Making requests with Basic Auth is easy, with Requests::219+Making requests with Basic Auth is extremely simple::220 221 >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))222 <Response [200]>223 224 225 Digest Authentication226 ---------------------227 170-Another popular form of protecting web service is Digest Authentication.228+Another popular form of protecting web service is Digest Authentication::229 172-Requests supports it!::173-230 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'231 >>> requests.get(url, auth=('digest', 'user', 'pass'))232 <Response [200]>233 4 unmodified lines