Merge pull request #2141 from esparta/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Cory Benfield · committed by Cory Benfield · parents 4c94282, 698b93b · Browse files · 2 files·+19 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Docs: Using print() function instead print command on advanced & quickstart
Diff stats
2 files+19 added−19 removed
- other:+19 added−19 removed
Provenance
- Commit
e66466a- Tree
0e1d421- Author
Cory Benfield <lukasaoz@gmail.com>- Committer
Cory Benfield <lukasaoz@gmail.com>- Parent 1
4c94282- Parent 2
698b93b
2 files changed+19 added−19 removed
modified:docs/user/advanced.rst+14 added-14 removed
359 unmodified lines360 for line in r.iter_lines():361 362 # filter out keep-alive new lines363 if line:364- print json.loads(line)364+ print(json.loads(line))365 366 367 Proxies368 -------75 unmodified lines444 We should confirm that GitHub responded correctly. If it has, we want to work445 out what type of content it is. Do this like so::446 447 >>> if r.status_code == requests.codes.ok:448- ... print r.headers['content-type']448+ ... print(r.headers['content-type'])449 ...450 application/json; charset=utf-8451 452 So, GitHub returns JSON. That's great, we can use the :meth:`r.json1 unmodified line454 455 ::456 457 >>> commit_data = r.json()458- >>> print commit_data.keys()458+ >>> print(commit_data.keys())459 [u'committer', u'author', u'url', u'tree', u'sha', u'parents', u'message']460- >>> print commit_data[u'committer']460+ >>> print(commit_data[u'committer'])461 {u'date': u'2012-05-10T11:10:50-07:00', u'email': u'me@kennethreitz.com', u'name': u'Kenneth Reitz'}462- >>> print commit_data[u'message']462+ >>> print(commit_data[u'message'])463 makin' history464 465 So far, so simple. Well, let's investigate the GitHub API a little bit. Now,466 we could look at the documentation, but we might have a little more fun if we14 unmodified lines481 482 ::483 484 >>> verbs = requests.options('http://a-good-website.com/api/cats')485- >>> print verbs.headers['allow']485+ >>> print(verbs.headers['allow'])486 GET,HEAD,POST,OPTIONS487 488 Turning to the documentation, we see that the only other method allowed for489 commits is POST, which creates a new commit. As we're using the Requests repo,8 unmodified lines498 >>> r = requests.get('https://api.github.com/repos/kennethreitz/requests/issues/482')499 >>> r.status_code500 200501 >>> issue = json.loads(r.text)502- >>> print issue[u'title']502+ >>> print(issue[u'title'])503 Feature any http verb in docs504- >>> print issue[u'comments']504+ >>> print(issue[u'comments'])505 3506 507 Cool, we have three comments. Let's take a look at the last of them.508 2 unmodified lines511 >>> r = requests.get(r.url + u'/comments')512 >>> r.status_code513 200514 >>> comments = r.json()515- >>> print comments[0].keys()515+ >>> print(comments[0].keys())516 [u'body', u'url', u'created_at', u'updated_at', u'user', u'id']517- >>> print comments[2][u'body']517+ >>> print(comments[2][u'body'])518 Probably in the "advanced" section519 520 Well, that seems like a silly place. Let's post a comment telling the poster521 that he's silly. Who is the poster, anyway?522 523 ::524 525- >>> print comments[2][u'user'][u'login']525+ >>> print(comments[2][u'user'][u'login'])526 kennethreitz527 528 OK, so let's tell this Kenneth guy that we think this example should go in the529 quickstart guide instead. According to the GitHub API doc, the way to do this18 unmodified lines548 >>> r = requests.post(url=url, data=body, auth=auth)549 >>> r.status_code550 201551 >>> content = r.json()552- >>> print content[u'body']552+ >>> print(content[u'body'])553 Sounds great! I'll get right on it.554 555 Brilliant. Oh, wait, no! I meant to add that it would take me a while, because556 I had to go feed my cat. If only I could edit this comment! Happily, GitHub1 unmodified line558 that.559 560 ::561 562- >>> print content[u"id"]562+ >>> print(content[u"id"])563 5804413564 >>> body = json.dumps({u"body": u"Sounds great! I'll get right on it once I feed my cat."})565 >>> url = u"https://api.github.com/repos/kennethreitz/requests/issues/comments/5804413"566 >>> r = requests.patch(url=url, data=body, auth=auth)20 unmodified lines587 588 ::589 590 >>> r = requests.head(url=url, auth=auth)591- >>> print r.headers591+ >>> print(r.headers)592 ...593 'x-ratelimit-remaining': '4995'594 'x-ratelimit-limit': '5000'595 ...110 unmodified linesmodified:docs/user/quickstart.rst+5 added-5 removed
192 unmodified lines193 dictionary of data will automatically be form-encoded when the request is made::194 195 >>> payload = {'key1': 'value1', 'key2': 'value2'}196 >>> r = requests.post("http://httpbin.org/post", data=payload)197- >>> print r.text197+ >>> print(r.text)198 {199 ...200 "form": {201 "key2": "value2",61 unmodified lines263 },264 ...265 }266 267-In the event you are posting a very large file as a ``multipart/form-data`` 268-request, you may want to stream the request. By default, ``requests`` does not 269-support this, but there is a separate package which does - 270-``requests-toolbelt``. You should read `the toolbelt's documentation 267+In the event you are posting a very large file as a ``multipart/form-data``268+request, you may want to stream the request. By default, ``requests`` does not269+support this, but there is a separate package which does -270+``requests-toolbelt``. You should read `the toolbelt's documentation271 <https://toolbelt.rtfd.org>`_ for more details about how to use it.272 273 274 Response Status Codes171 unmodified lines