quickstart.rst
This file
Path
docs/user/quickstart.rst
134 lines · 3.2 KiB
Last changed by
typoKenneth Reitzbefore adoption5f603ae
History of this fileTake it
1.. _quickstart:23Quickstart4==========56.. module:: requests.models78Eager to get started? This page gives a good introduction in how to get started with Requests. This assumes you already have Requests installed. If you do not, head over to the :ref:`Installation <install>` section.910First, make sure that:1112* Requests is :ref:`installed <install>`13* Requests is :ref:`up-to-date <updates>`141516Lets gets started with some simple use cases and examples.171819Make a GET Request20------------------2122Making a standard request with Requests is very simple.2324Let's get GitHub's public timeline ::2526 r = requests.get('https://github.com/timeline.json')2728Now, we have a :class:`Response` object called ``r``. We can get all the information we need from this.293031Response Content32----------------3334We can read the content of the server's response::3536 >>> r.content37 '[{"repository":{"open_issues":0,"url":"https://github.com/...383940Response Status Codes41---------------------4243We can check the response status code::4445 >>> r.status_code46 2004748Requests also comes with a built-in status code lookup object for easy49reference::5051 >>> r.status_code == requests.codes.ok52 True5354If we made a bad request, we can raise it with55:class:`Response.raise_for_status()`::5657 >>> _r = requests.get('http://httpbin.org/status/404')58 >>> _r.status_code59 4046061 >>> _r.raise_for_status()62 Traceback (most recent call last):63 File "requests/models.py", line 394, in raise_for_status64 raise self.error65 urllib2.HTTPError: HTTP Error 404: NOT FOUND6667But, since our ``status_code`` was ``200``, when we call it::6869 >>> r.raise_for_status()70 None7172All is well.737475Response Headers76----------------7778We can view the server's response headers with a simple Python dictionary79interface::8081 >>> r.headers82 {83 'status': '200 OK',84 'content-encoding': 'gzip',85 'transfer-encoding': 'chunked',86 'connection': 'close',87 'server': 'nginx/1.0.4',88 'x-runtime': '148ms',89 'etag': '"e1ca502697e5c9317743dc078f67693f"',90 'content-type': 'application/json; charset=utf-8'91 }9293The dictionary is special, though: it's made just for HTTP headers. According to `RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTP94Headers are case-insensitive.9596So, we can access the headers using any capitalization we want::9798 >>> r.headers['Content-Type']99 'application/json; charset=utf-8'100101 >>> r.headers.get('content-type')102 'application/json; charset=utf-8'103104If a header doesn't exist in the Response, its value defaults to ``None``::105106 >>> r.headers['X-Random']107 None108109110Cookies111-------112113If a response contains some Cookies, you can get quick access to them::114115 >>> url = 'http://httpbin.org/cookies/set/requests-is/awesome'116 >>> r = requests.get(url)117118 >>> print r.cookies119 {'requests-is': 'awesome'}120121The underlying CookieJar is also available for more advanced handing::122123 >>> r.request.cookiejar124 <cookielib.CookieJar>125126To send your own cookies to the server, you can use the ``cookies``127parameter::128129 >>> url = 'http://httpbin.org/cookies'130 >>> cookies = dict(cookies_are='working')131132 >>> r = requests.get(url, cookies=cookies)133 >>> r.content134 '{"cookies": {"cookies_are": "working"}}'