Skip to content
Myr

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

1 file+22 added−8 removed

modified:docs/user/quickstart.rst+22 added-8 removed
35 unmodified lines
3637Requests' simple API means that all forms of HTTP request are as obvious. For38example, this is how you make an HTTP POST request::3940 >>> r = requests.post("http://httpbin.org/post", data = {"key":"value"})40 >>> r = requests.post('http://httpbin.org/post', data = {'key':'value'})4142Nice, right? What about the other HTTP request types: PUT, DELETE, HEAD and43OPTIONS? These are all just as simple::4445 >>> 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')4950That's all well and good, but it's also only the start of what Requests can51do.52
9 unmodified lines
62``key1=value1`` and ``key2=value2`` to ``httpbin.org/get``, you would use the63following code::6465 >>> 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)6768You can see that the URL has been correctly encoded by printing the URL::6970 >>> print(r.url)
4 unmodified lines
7576You can also pass a list of items as a value::7778 >>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}7979 >>> 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=value38384Response Content
2 unmodified lines
87We can read the content of the server's response. Consider the GitHub timeline88again::8990 >>> import requests9192 >>> r = requests.get('https://api.github.com/events')93 >>> r.text94 u'[{"repository":{"open_issues":0,"url":"https://github.com/...95
36 unmodified lines
132use the following code::133134 >>> from PIL import Image135 >>> from StringIO import StringIO136137 >>> i = Image.open(StringIO(r.content))138139140JSON Response Content
1 unmodified line
142143There's also a builtin JSON decoder, in case you're dealing with JSON data::144145 >>> import requests146147 >>> 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 lines
166server, 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::168169 >>> r = requests.get('https://api.github.com/events', stream=True)170171 >>> r.raw172 <requests.packages.urllib3.response.HTTPResponse object at 0x101194810>173174 >>> r.raw.read(10)175 '\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'176177In general, however, you should use a pattern like this to save what is being
16 unmodified lines
194``headers`` parameter.195196For example, we didn't specify our user-agent in the previous example::197192 >>> import json198 >>> url = 'https://api.github.com/some/endpoint'199 >>> headers = {'user-agent': 'my-app/0.0.1'}200201 >>> r = requests.get(url, headers=headers)
17 unmodified lines
219To do this, simply pass a dictionary to the ``data`` argument. Your220dictionary of data will automatically be form-encoded when the request is made::221222 >>> payload = {'key1': 'value1', 'key2': 'value2'}223224 >>> r = requests.post("http://httpbin.org/post", data=payload)225 >>> print(r.text)226 {227 ...
9 unmodified lines
237238For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data::239240 >>> import json241242 >>> url = 'https://api.github.com/some/endpoint'243 >>> payload = {'some': 'data'}244245 >>> r = requests.post(url, data=json.dumps(payload))
186 unmodified lines
432433For example, GitHub redirects all HTTP requests to HTTPS::434435 >>> r = requests.get('http://github.com')436437 >>> r.url438 'https://github.com/'439440 >>> r.status_code441 200442443 >>> r.history444 [<Response [301]>]445446447If you're using GET, OPTIONS, POST, PUT, PATCH or DELETE, you can disable448redirection handling with the ``allow_redirects`` parameter::449450 >>> r = requests.get('http://github.com', allow_redirects=False)451452 >>> r.status_code453 301454455 >>> r.history456 []457458If you're using HEAD, you can enable redirection as well::459460 >>> r = requests.head('http://github.com', allow_redirects=True)461462 >>> r.url463 'https://github.com/'464465 >>> r.history466 [<Response [301]>]467468
41 unmodified lines