quickstart.rst
This file
Path
docs/user/quickstart.rst
254 lines · 6.5 KiB
Last changed by
file uploadsKenneth Reitzbefore adoption40c5480
History of this fileTake it
Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 1why, one step back1.. _quickstart:Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 2why, one step back2Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 3–4why, one step back3Quickstart4==========Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 5why, one step back5Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 6why, one step back6.. module:: requests.modelsMerge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 7why, one step back7Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef
lines 8–10why, one step back8Eager to get started? This page gives a good introduction in how to get started9with Requests. This assumes you already have Requests installed. If you do not,10head over to the :ref:`Installation <install>` section.Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 11why, one step back11Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 12why, one step back12First, make sure that:Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 13why, one step back13Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 14–15why, one step back14* Requests is :ref:`installed <install>`15* Requests is :ref:`up-to-date <updates>`Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
lines 16–17why, one step back1617Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 18why, one step back18Lets gets started with some simple use cases and examples.Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
lines 19–20why, one step back1920Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 21–22why, one step back21Make a GET Request22------------------Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 23why, one step back23Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 24why, one step back24Making a standard request with Requests is very simple.Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 25why, one step back25Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 26why, one step back26Let's get GitHub's public timeline ::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 27why, one step back27Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 28why, one step back28 r = requests.get('https://github.com/timeline.json')Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 29why, one step back29Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef
lines 30–31why, one step back30Now, we have a :class:`Response` object called ``r``. We can get all the31information we need from this.Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
lines 32–33why, one step back3233Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 34–35why, one step back34Response Content35----------------Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 36why, one step back36Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 37why, one step back37We can read the content of the server's response::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 38why, one step back38Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 39–40why, one step back39 >>> r.content40 '[{"repository":{"open_issues":0,"url":"https://github.com/...Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 41why, one step back41big quick start updatesKenneth Reitzbefore adoption60558fd
line 42why, one step back42Requests does its best to decode content from the server. Most unicode charsets, ``gzip``, and ``deflate`` encodings are all seamlessly decoded.Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 43why, one step back43big quick start updatesKenneth Reitzbefore adoption60558fd
line 44why, one step back44Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a
line 45why, one step back45Make a POST Requestbig quick start updatesKenneth Reitzbefore adoption60558fd
line 46why, one step back46-------------------Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a
line 47why, one step back47big quick start updatesKenneth Reitzbefore adoption60558fd
line 48why, one step back48POST requests are equally simple::Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a
line 49why, one step back49big quick start updatesKenneth Reitzbefore adoption60558fd
line 50why, one step back50 r = requests.post("http://httpbin.org/post")Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a
lines 51–52why, one step back5152big quick start updatesKenneth Reitzbefore adoption60558fd
lines 53–111why, one step back53Typically, 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')}111file uploadsKenneth Reitzbefore adoption40c5480
line 112why, one step back112 >>> r = requests.post(url, files=files)Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a
line 113why, one step back113 >>> r.contentfile uploadsKenneth Reitzbefore adoption40c5480
lines 114–131why, one step back114 {115 "origin": "179.13.100.4",116 "files": {117 "hmm": "<censored...binary...data>"118 },119 "form": {},120 "url": "http://httpbin.org/post",121 "args": {},122 "headers": {123 "Content-Length": "3196",124 "Accept-Encoding": "identity, deflate, compress, gzip",125 "Accept": "*/*",126 "User-Agent": "python-requests/0.8.0",127 "Host": "httpbin.org:80",128 "Content-Type": "multipart/form-data; boundary=127.0.0.1.502.21746.1321131593.786.1"129 },130 "data": ""131 }Merge branch 'develop' of https://github.com/joequery/requests into developKenneth Reitzbefore adoption3d6937a
line 132why, one step back132big quick start updatesKenneth Reitzbefore adoption60558fd
lines 133–136why, one step back133134135136Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 137–138why, one step back137Response Status Codes138---------------------Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 139why, one step back139Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 140why, one step back140We can check the response status code::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 141why, one step back141Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 142–143why, one step back142 >>> r.status_code143 200Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 144why, one step back144Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 145–146why, one step back145Requests also comes with a built-in status code lookup object for easy146reference::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 147why, one step back147Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 148–149why, one step back148 >>> r.status_code == requests.codes.ok149 TrueMerge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 150why, one step back150big quick start updatesKenneth Reitzbefore adoption60558fd
line 151why, one step back151If we made a bad request (non-200 response), we can raise it withMerge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 152why, one step back152:class:`Response.raise_for_status()`::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 153why, one step back153Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 154–156why, one step back154 >>> _r = requests.get('http://httpbin.org/status/404')155 >>> _r.status_code156 404Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 157why, one step back157Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 158–162why, one step back158 >>> _r.raise_for_status()159 Traceback (most recent call last):160 File "requests/models.py", line 394, in raise_for_status161 raise self.error162 urllib2.HTTPError: HTTP Error 404: NOT FOUNDMerge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 163why, one step back163Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 164why, one step back164But, since our ``status_code`` was ``200``, when we call it::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 165why, one step back165Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 166–167why, one step back166 >>> r.raise_for_status()167 NoneMerge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 168why, one step back168Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 169why, one step back169All is well.Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
lines 170–171why, one step back170171Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 172–173why, one step back172Response Headers173----------------Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 174why, one step back174Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 175–176why, one step back175We can view the server's response headers with a simple Python dictionary176interface::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 177why, one step back177Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 178–188why, one step back178 >>> r.headers179 {180 'status': '200 OK',181 'content-encoding': 'gzip',182 'transfer-encoding': 'chunked',183 'connection': 'close',184 'server': 'nginx/1.0.4',185 'x-runtime': '148ms',186 'etag': '"e1ca502697e5c9317743dc078f67693f"',187 'content-type': 'application/json; charset=utf-8'188 }Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 189why, one step back189Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef
lines 190–191why, one step back190The dictionary is special, though: it's made just for HTTP headers. According to191`RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTPMerge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 192why, one step back192Headers are case-insensitive.Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 193why, one step back193Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 194why, one step back194So, we can access the headers using any capitalization we want::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 195why, one step back195Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 196–227why, one step back196 >>> r.headers['Content-Type']197 'application/json; charset=utf-8'198199 >>> r.headers.get('content-type')200 'application/json; charset=utf-8'201202If a header doesn't exist in the Response, its value defaults to ``None``::203204 >>> r.headers['X-Random']205 None206207208Cookies209-------210211If a response contains some Cookies, you can get quick access to them::212213 >>> url = 'http://httpbin.org/cookies/set/requests-is/awesome'214 >>> r = requests.get(url)215216 >>> print r.cookies217 {'requests-is': 'awesome'}218219To send your own cookies to the server, you can use the ``cookies``220parameter::221222 >>> url = 'http://httpbin.org/cookies'223 >>> cookies = dict(cookies_are='working')224225 >>> r = requests.get(url, cookies=cookies)226 >>> r.content227 '{"cookies": {"cookies_are": "working"}}'Merge branch 'develop' into feature/rewriteKenneth Reitzbefore adoption95336a0
lines 228–235why, one step back228229230Basic Authentication231--------------------232233Most web services require authentication. There many different types of234authentication, but the most common is called HTTP Basic Auth.235big quick start updatesKenneth Reitzbefore adoption60558fd
line 236why, one step back236Making requests with Basic Auth is extremely simple::Merge branch 'develop' into feature/rewriteKenneth Reitzbefore adoption95336a0
lines 237–244why, one step back237238 >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))239 <Response [200]>240241242Digest Authentication243---------------------244big quick start updatesKenneth Reitzbefore adoption60558fd
line 245why, one step back245Another popular form of protecting web service is Digest Authentication::Merge branch 'develop' into feature/rewriteKenneth Reitzbefore adoption95336a0
lines 246–253why, one step back246247 >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'248 >>> requests.get(url, auth=('digest', 'user', 'pass'))249 <Response [200]>250251252-----------------------253Merge branch 'develop' into feature/rewriteKenneth Reitzbefore adoptionc48f0d9
line 254why, one step back254Ready for more? Check out the :ref:`advanced <advanced>` section.