Skip to content
Myr

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

2 files+19 added−19 removed

modified:docs/user/advanced.rst+14 added-14 removed
359 unmodified lines
360 for line in r.iter_lines():361362 # filter out keep-alive new lines363 if line:364 print json.loads(line)364 print(json.loads(line))365366367Proxies368-------
75 unmodified lines
444We should confirm that GitHub responded correctly. If it has, we want to work445out what type of content it is. Do this like so::446447 >>> 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-8451452So, GitHub returns JSON. That's great, we can use the :meth:`r.json
1 unmodified line
454455::456457 >>> 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' history464465So far, so simple. Well, let's investigate the GitHub API a little bit. Now,466we could look at the documentation, but we might have a little more fun if we
14 unmodified lines
481482::483484 >>> verbs = requests.options('http://a-good-website.com/api/cats')485 >>> print verbs.headers['allow']485 >>> print(verbs.headers['allow'])486 GET,HEAD,POST,OPTIONS487488Turning to the documentation, we see that the only other method allowed for489commits is POST, which creates a new commit. As we're using the Requests repo,
8 unmodified lines
498 >>> 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 3506507Cool, we have three comments. Let's take a look at the last of them.508
2 unmodified lines
511 >>> 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" section519520Well, that seems like a silly place. Let's post a comment telling the poster521that he's silly. Who is the poster, anyway?522523::524525 >>> print comments[2][u'user'][u'login']525 >>> print(comments[2][u'user'][u'login'])526 kennethreitz527528OK, so let's tell this Kenneth guy that we think this example should go in the529quickstart guide instead. According to the GitHub API doc, the way to do this
18 unmodified lines
548 >>> 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.554555Brilliant. Oh, wait, no! I meant to add that it would take me a while, because556I had to go feed my cat. If only I could edit this comment! Happily, GitHub
1 unmodified line
558that.559560::561562 >>> 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 lines
587588::589590 >>> 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 lines
modified:docs/user/quickstart.rst+5 added-5 removed
192 unmodified lines
193dictionary of data will automatically be form-encoded when the request is made::194195 >>> 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 lines
263 },264 ...265 }266267In the event you are posting a very large file as a ``multipart/form-data`` 268request, you may want to stream the request. By default, ``requests`` does not 269support this, but there is a separate package which does - 270``requests-toolbelt``. You should read `the toolbelt's documentation 267In the event you are posting a very large file as a ``multipart/form-data``268request, you may want to stream the request. By default, ``requests`` does not269support 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.272273274Response Status Codes
171 unmodified lines