Skip to content
Myr

quickstart.rst

This file

Path

docs/user/quickstart.rst

134 lines · 3.2 KiB

Last changed by

typoKenneth Reitzbefore adoption5f603ae

History of this file

Take it

The walk stopped after 200 commits: lines older than 0b36d92 point at it.

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 1why, one step back
1.. _quickstart:

remove debug printKenneth Reitzbefore adoption0b36d92

lines 2–5why, one step back
2
3Quickstart4==========5

models..Kenneth Reitzbefore adoption2cf0dad

line 6why, one step back
6.. module:: requests.models

remove debug printKenneth Reitzbefore adoption0b36d92

line 7why, one step back
7

not tablib..Kenneth Reitzbefore adoption414b9dd

line 8why, one step back
8Eager 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.

remove debug printKenneth Reitzbefore adoption0b36d92

line 9why, one step back
9

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 10why, one step back
10First, make sure that:

remove debug printKenneth Reitzbefore adoption0b36d92

line 11why, one step back
11

whoopsKenneth Reitzbefore adoption49e5df1

lines 12–13why, one step back
12* Requests is :ref:`installed <install>`
13* Requests is :ref:`up-to-date <updates>`

remove debug printKenneth Reitzbefore adoption0b36d92

lines 14–15why, one step back
14
15

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 16why, one step back
16Lets gets started with some simple use cases and examples.

remove debug printKenneth Reitzbefore adoption0b36d92

lines 17–18why, one step back
17
18

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 19why, one step back
19Make a GET Request

remove debug printKenneth Reitzbefore adoption0b36d92

lines 20–21why, one step back
20------------------
21

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 22why, one step back
22Making a standard request with Requests is very simple.

remove debug printKenneth Reitzbefore adoption0b36d92

line 23why, one step back
23

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 24why, one step back
24Let's get GitHub's public timeline ::

remove debug printKenneth Reitzbefore adoption0b36d92

line 25why, one step back
25

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 26why, one step back
26 r = requests.get('https://github.com/timeline.json')

remove debug printKenneth Reitzbefore adoption0b36d92

line 27why, one step back
27

raise_for_status quick startKenneth Reitzbefore adoptionf137db6

line 28why, one step back
28Now, we have a :class:`Response` object called ``r``. We can get all the information we need from this.

remove debug printKenneth Reitzbefore adoption0b36d92

lines 29–30why, one step back
29
30

massive documentation updateKenneth Reitzbefore adoptiond50742f

lines 31–32why, one step back
31Response Content
32----------------

remove debug printKenneth Reitzbefore adoption0b36d92

line 33why, one step back
33

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 34why, one step back
34We can read the content of the server's response::

remove debug printKenneth Reitzbefore adoption0b36d92

line 35why, one step back
35

massive documentation updateKenneth Reitzbefore adoptiond50742f

lines 36–37why, one step back
36 >>> r.content
37 '[{"repository":{"open_issues":0,"url":"https://github.com/...

remove debug printKenneth Reitzbefore adoption0b36d92

lines 38–39why, one step back
38
39

massive documentation updateKenneth Reitzbefore adoptiond50742f

lines 40–41why, one step back
40Response Status Codes
41---------------------

remove debug printKenneth Reitzbefore adoption0b36d92

line 42why, one step back
42

massive documentation updateKenneth Reitzbefore adoptiond50742f

line 43why, one step back
43We can check the response status code::

remove debug printKenneth Reitzbefore adoption0b36d92

line 44why, one step back
44

massive documentation updateKenneth Reitzbefore adoptiond50742f

lines 45–46why, one step back
45 >>> r.status_code
46 200

remove debug printKenneth Reitzbefore adoption0b36d92

line 47why, one step back
47

massive documentation updateKenneth Reitzbefore adoptiond50742f

lines 48–52why, one step back
48Requests also comes with a built-in status code lookup object for easy
49reference::5051 >>> r.status_code == requests.codes.ok52 True

raise_for_status quick startKenneth Reitzbefore adoptionf137db6

lines 53–66why, one step back
53
54If 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 FOUND66

typoKenneth Reitzbefore adoption5f603ae

line 67why, one step back
67But, since our ``status_code`` was ``200``, when we call it::

raise_for_status quick startKenneth Reitzbefore adoptionf137db6

lines 68–72why, one step back
68
69 >>> r.raise_for_status()70 None7172All is well.

Response Headers quickstartKenneth Reitzbefore adoption2357b13

lines 73–107why, one step back
73
7475Response 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 None

cookies docsKenneth Reitzbefore adoption3992ed6

lines 108–134why, one step back
108
109110Cookies111-------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"}}'