Skip to content
Myr

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

1 file+72 added−16 removed

modified:docs/user/quickstart.rst+72 added-16 removed
37 unmodified lines
3839 >>> r.content40 '[{"repository":{"open_issues":0,"url":"https://github.com/...4142Requests does its best to decode content from the server. Most unicode charsets, ``gzip``, and ``deflate`` encodings are all seamlessly decoded.434445Make a POST Request44------------------46-------------------4746POST requests are equally simple ::48POST requests are equally simple::4948 >>> r = requests.post("http://httpbin.org/post")49 50 r = requests.post("http://httpbin.org/post")5151Suppose you want to send data over HTTP. Simply pass a data52argument to the requests.post method with your dictionary ::5254 >>> dataDict = {"key1":"value1", "key2":"value2"}55 >>> r = requests.post("http://httpbin.org/post", data=dataDict)53Typically, you want to send some form-encoded data — much like an HTML form.54To do this, simply pass a dictionary to the `data` argument. Your dictionary of data will automatically be form-encoded when the request is made::5556 >>> 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 }7879There 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.8081For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data::8283 url = 'https://api.github.com/some/endpoint'84 payload = {'some': 'data'}8586 r = requests.post(url, data=json.dumps(payload))878889Custom Headers90--------------9192If you'd like to add HTTP headers to a request, simply pass in a ``dict`` to the93``headers`` parameter.9495For example, we didn't specify our content-type in the previous example::9697 url = 'https://api.github.com/some/endpoint'98 payload = {'some': 'data'}99 headers = {'content-type': 'application/json'}100101 r = requests.post(url, data=json.dumps(payload), headers=headers)102103104POST a Multipart-Encoded File105-----------------------------106107Requests makes it simple to upload Multipart-encoded files::108109 >>> url = 'http://httpbin.org/post'110 >>> files = {'report.xls': open('report.xls', 'rb')}111112 >>> 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 }, ...11459Note the data= argument is equivalent to -d in cURL scripts. dataDict will60be form-encoded. 115116117118119120Response Status Codes121---------------------122123We can check the response status code::
6 unmodified lines
130131 >>> r.status_code == requests.codes.ok132 True13376If we made a bad request, we can raise it with134If we made a bad request (non-200 response), we can raise it with135:class:`Response.raise_for_status()`::136137 >>> _r = requests.get('http://httpbin.org/status/404')138 >>> _r.status_code
76 unmodified lines
215216Most web services require authentication. There many different types of217authentication, but the most common is called HTTP Basic Auth.218161Making requests with Basic Auth is easy, with Requests::219Making requests with Basic Auth is extremely simple::220221 >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))222 <Response [200]>223224225Digest Authentication226---------------------227170Another popular form of protecting web service is Digest Authentication.228Another popular form of protecting web service is Digest Authentication::229172Requests supports it!::173230 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'231 >>> requests.get(url, auth=('digest', 'user', 'pass'))232 <Response [200]>233
4 unmodified lines