quickstart.rst
This file
Path
docs/user/quickstart.rst
138 lines · 3.2 KiB
Last changed by
Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef
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
lines 41–42why, one step back4142Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 43–44why, one step back43Response Status Codes44---------------------Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 45why, one step back45Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 46why, one step back46We can check the response status code::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 47why, one step back47Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 48–49why, one step back48 >>> r.status_code49 200Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 50why, one step back50Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 51–52why, one step back51Requests also comes with a built-in status code lookup object for easy52reference::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 53why, one step back53Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 54–55why, one step back54 >>> r.status_code == requests.codes.ok55 TrueMerge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 56why, one step back56Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 57–58why, one step back57If we made a bad request, we can raise it with58:class:`Response.raise_for_status()`::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 59why, one step back59Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 60–62why, one step back60 >>> _r = requests.get('http://httpbin.org/status/404')61 >>> _r.status_code62 404Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 63why, one step back63Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 64–68why, one step back64 >>> _r.raise_for_status()65 Traceback (most recent call last):66 File "requests/models.py", line 394, in raise_for_status67 raise self.error68 urllib2.HTTPError: HTTP Error 404: NOT FOUNDMerge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 69why, one step back69Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 70why, one step back70But, since our ``status_code`` was ``200``, when we call it::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 71why, one step back71Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 72–73why, one step back72 >>> r.raise_for_status()73 NoneMerge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 74why, one step back74Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 75why, one step back75All is well.Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
lines 76–77why, one step back7677Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 78–79why, one step back78Response Headers79----------------Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 80why, one step back80Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 81–82why, one step back81We can view the server's response headers with a simple Python dictionary82interface::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 83why, one step back83Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 84–94why, one step back84 >>> r.headers85 {86 'status': '200 OK',87 'content-encoding': 'gzip',88 'transfer-encoding': 'chunked',89 'connection': 'close',90 'server': 'nginx/1.0.4',91 'x-runtime': '148ms',92 'etag': '"e1ca502697e5c9317743dc078f67693f"',93 'content-type': 'application/json; charset=utf-8'94 }Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 95why, one step back95Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef
lines 96–97why, one step back96The dictionary is special, though: it's made just for HTTP headers. According to97`RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTPMerge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 98why, one step back98Headers are case-insensitive.Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 99why, one step back99Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
line 100why, one step back100So, we can access the headers using any capitalization we want::Merge branch 'release/0.4.1'Kenneth Reitzbefore adoptionadd6fea
line 101why, one step back101Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 102–124why, one step back102 >>> r.headers['Content-Type']103 'application/json; charset=utf-8'104105 >>> r.headers.get('content-type')106 'application/json; charset=utf-8'107108If a header doesn't exist in the Response, its value defaults to ``None``::109110 >>> r.headers['X-Random']111 None112113114Cookies115-------116117If a response contains some Cookies, you can get quick access to them::118119 >>> url = 'http://httpbin.org/cookies/set/requests-is/awesome'120 >>> r = requests.get(url)121122 >>> print r.cookies123 {'requests-is': 'awesome'}124Migrate new docs over.Kenneth Reitzbefore adoption4a7fdef
line 125why, one step back125The underlying CookieJar is also available for more advanced handling::Merge branch 'develop'Kenneth Reitzbefore adoption81d5997
lines 126–138why, one step back126127 >>> r.request.cookiejar128 <cookielib.CookieJar>129130To send your own cookies to the server, you can use the ``cookies``131parameter::132133 >>> url = 'http://httpbin.org/cookies'134 >>> cookies = dict(cookies_are='working')135136 >>> r = requests.get(url, cookies=cookies)137 >>> r.content138 '{"cookies": {"cookies_are": "working"}}'