# filter out keep-alive new lines
if line:
- print json.loads(line)
+ print(json.loads(line))
Proxies
out what type of content it is. Do this like so::
>>> if r.status_code == requests.codes.ok:
- ... print r.headers['content-type']
+ ... print(r.headers['content-type'])
...
application/json; charset=utf-8
::
>>> commit_data = r.json()
- >>> print commit_data.keys()
+ >>> print(commit_data.keys())
[u'committer', u'author', u'url', u'tree', u'sha', u'parents', u'message']
- >>> print commit_data[u'committer']
+ >>> print(commit_data[u'committer'])
{u'date': u'2012-05-10T11:10:50-07:00', u'email': u'me@kennethreitz.com', u'name': u'Kenneth Reitz'}
- >>> print commit_data[u'message']
+ >>> print(commit_data[u'message'])
makin' history
So far, so simple. Well, let's investigate the GitHub API a little bit. Now,
::
>>> verbs = requests.options('http://a-good-website.com/api/cats')
- >>> print verbs.headers['allow']
+ >>> print(verbs.headers['allow'])
GET,HEAD,POST,OPTIONS
Turning to the documentation, we see that the only other method allowed for
>>> r.status_code
200
>>> issue = json.loads(r.text)
- >>> print issue[u'title']
+ >>> print(issue[u'title'])
Feature any http verb in docs
- >>> print issue[u'comments']
+ >>> print(issue[u'comments'])
3
Cool, we have three comments. Let's take a look at the last of them.
>>> r.status_code
200
>>> comments = r.json()
- >>> print comments[0].keys()
+ >>> print(comments[0].keys())
[u'body', u'url', u'created_at', u'updated_at', u'user', u'id']
- >>> print comments[2][u'body']
+ >>> print(comments[2][u'body'])
Probably in the "advanced" section
Well, that seems like a silly place. Let's post a comment telling the poster
::
- >>> print comments[2][u'user'][u'login']
+ >>> print(comments[2][u'user'][u'login'])
kennethreitz
OK, so let's tell this Kenneth guy that we think this example should go in the
>>> r.status_code
201
>>> content = r.json()
- >>> print content[u'body']
+ >>> print(content[u'body'])
Sounds great! I'll get right on it.
Brilliant. Oh, wait, no! I meant to add that it would take me a while, because
::
- >>> print content[u"id"]
+ >>> print(content[u"id"])
5804413
>>> body = json.dumps({u"body": u"Sounds great! I'll get right on it once I feed my cat."})
>>> url = u"https://api.github.com/repos/kennethreitz/requests/issues/comments/5804413"
::
>>> r = requests.head(url=url, auth=auth)
- >>> print r.headers
+ >>> print(r.headers)
...
'x-ratelimit-remaining': '4995'
'x-ratelimit-limit': '5000'
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)
- >>> print r.text
+ >>> print(r.text)
{
...
"form": {
...
}
-In the event you are posting a very large file as a ``multipart/form-data``
-request, you may want to stream the request. By default, ``requests`` does not
-support this, but there is a separate package which does -
-``requests-toolbelt``. You should read `the toolbelt's documentation
+In the event you are posting a very large file as a ``multipart/form-data``
+request, you may want to stream the request. By default, ``requests`` does not
+support this, but there is a separate package which does -
+``requests-toolbelt``. You should read `the toolbelt's documentation
<https://toolbelt.rtfd.org>`_ for more details about how to use it.