Skip to content
Myr

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 file

Take it

1.. _quickstart:23Quickstart4==========56.. module:: requests.models78Eager 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.1112First, make sure that:1314* Requests is :ref:`installed <install>`15* Requests is :ref:`up-to-date <updates>`161718Lets gets started with some simple use cases and examples.192021Make a GET Request22------------------2324Making a standard request with Requests is very simple.2526Let's get GitHub's public timeline ::2728    r = requests.get('https://github.com/timeline.json')2930Now, we have a :class:`Response` object called ``r``. We can get all the31information we need from this.323334Response Content35----------------3637We can read the content of the server's response::3839    >>> r.content40    '[{"repository":{"open_issues":0,"url":"https://github.com/...414243Response Status Codes44---------------------4546We can check the response status code::4748    >>> r.status_code49    2005051Requests also comes with a built-in status code lookup object for easy52reference::5354    >>> r.status_code == requests.codes.ok55    True5657If we made a bad request, we can raise it with58:class:`Response.raise_for_status()`::5960    >>> _r = requests.get('http://httpbin.org/status/404')61    >>> _r.status_code62    4046364    >>> _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 FOUND6970But, since our ``status_code`` was ``200``, when we call it::7172    >>> r.raise_for_status()73    None7475All is well.767778Response Headers79----------------8081We can view the server's response headers with a simple Python dictionary82interface::8384    >>> 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    }9596The dictionary is special, though: it's made just for HTTP headers. According to97`RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>`_, HTTP98Headers are case-insensitive.99100So, we can access the headers using any capitalization we want::101102    >>> 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'}124125The underlying CookieJar is also available for more advanced handling::126127    >>> 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"}}'