cleanup quickstart
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 f4f863f · Browse files · 1 file·+22 added−8 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+22 added−8 removed
- other:+22 added−8 removed
Provenance
- Commit
bf8c831- Tree
f3c8a88- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent
f4f863f
1 file changed+22 added−8 removed
modified:docs/user/quickstart.rst+22 added-8 removed
35 unmodified lines36 37 Requests' simple API means that all forms of HTTP request are as obvious. For38 example, this is how you make an HTTP POST request::39 40- >>> r = requests.post("http://httpbin.org/post", data = {"key":"value"})40+ >>> r = requests.post('http://httpbin.org/post', data = {'key':'value'})41 42 Nice, right? What about the other HTTP request types: PUT, DELETE, HEAD and43 OPTIONS? These are all just as simple::44 45- >>> r = requests.put("http://httpbin.org/put", data = {"key":"value"})46- >>> r = requests.delete("http://httpbin.org/delete")47- >>> r = requests.head("http://httpbin.org/get")48- >>> r = requests.options("http://httpbin.org/get")45+ >>> r = requests.put('http://httpbin.org/put', data = {'key':'value'})46+ >>> r = requests.delete('http://httpbin.org/delete')47+ >>> r = requests.head('http://httpbin.org/get')48+ >>> r = requests.options('http://httpbin.org/get')49 50 That's all well and good, but it's also only the start of what Requests can51 do.52 9 unmodified lines62 ``key1=value1`` and ``key2=value2`` to ``httpbin.org/get``, you would use the63 following code::64 65 >>> payload = {'key1': 'value1', 'key2': 'value2'}66- >>> r = requests.get("http://httpbin.org/get", params=payload)66+ >>> r = requests.get('http://httpbin.org/get', params=payload)67 68 You can see that the URL has been correctly encoded by printing the URL::69 70 >>> print(r.url)4 unmodified lines75 76 You can also pass a list of items as a value::77 78 >>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}79+79- >>> r = requests.get("http://httpbin.org/get", params=payload)80+ >>> r = requests.get('http://httpbin.org/get', params=payload)81 >>> print(r.url)82 http://httpbin.org/get?key1=value1&key2=value2&key2=value383 84 Response Content2 unmodified lines87 We can read the content of the server's response. Consider the GitHub timeline88 again::89 90 >>> import requests91+92 >>> r = requests.get('https://api.github.com/events')93 >>> r.text94 u'[{"repository":{"open_issues":0,"url":"https://github.com/...95 36 unmodified lines132 use the following code::133 134 >>> from PIL import Image135 >>> from StringIO import StringIO136+137 >>> i = Image.open(StringIO(r.content))138 139 140 JSON Response Content1 unmodified line142 143 There's also a builtin JSON decoder, in case you're dealing with JSON data::144 145 >>> import requests146+147 >>> r = requests.get('https://api.github.com/events')148 >>> r.json()149 [{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...150 15 unmodified lines166 server, you can access ``r.raw``. If you want to do this, make sure you set167 ``stream=True`` in your initial request. Once you do, you can do this::168 169 >>> r = requests.get('https://api.github.com/events', stream=True)170+171 >>> r.raw172 <requests.packages.urllib3.response.HTTPResponse object at 0x101194810>173+174 >>> r.raw.read(10)175 '\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'176 177 In general, however, you should use a pattern like this to save what is being16 unmodified lines194 ``headers`` parameter.195 196 For example, we didn't specify our user-agent in the previous example::197 192- >>> import json198 >>> url = 'https://api.github.com/some/endpoint'199 >>> headers = {'user-agent': 'my-app/0.0.1'}200 201 >>> r = requests.get(url, headers=headers)17 unmodified lines219 To do this, simply pass a dictionary to the ``data`` argument. Your220 dictionary of data will automatically be form-encoded when the request is made::221 222 >>> payload = {'key1': 'value1', 'key2': 'value2'}223+224 >>> r = requests.post("http://httpbin.org/post", data=payload)225 >>> print(r.text)226 {227 ...9 unmodified lines237 238 For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data::239 240 >>> import json241+242 >>> url = 'https://api.github.com/some/endpoint'243 >>> payload = {'some': 'data'}244 245 >>> r = requests.post(url, data=json.dumps(payload))186 unmodified lines432 433 For example, GitHub redirects all HTTP requests to HTTPS::434 435 >>> r = requests.get('http://github.com')436+437 >>> r.url438 'https://github.com/'439+440 >>> r.status_code441 200442+443 >>> r.history444 [<Response [301]>]445 446 447 If you're using GET, OPTIONS, POST, PUT, PATCH or DELETE, you can disable448 redirection handling with the ``allow_redirects`` parameter::449 450 >>> r = requests.get('http://github.com', allow_redirects=False)451+452 >>> r.status_code453 301454+455 >>> r.history456 []457 458 If you're using HEAD, you can enable redirection as well::459 460 >>> r = requests.head('http://github.com', allow_redirects=True)461+462 >>> r.url463 'https://github.com/'464+465 >>> r.history466 [<Response [301]>]467 468 41 unmodified lines