Skip to content
Myr

Merge pull request #1085 from graingert/patch-1

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Update the advanced doc to use the r.json method

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 21616a8, 7687746 · Browse files · 1 file+5 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+5 added−10 removed

  • other:+5 added−10 removed

1 file+5 added−10 removed

modified:docs/user/advanced.rst+5 added-10 removed
311 unmodified lines
312 ... print r.headers['content-type']313 ...314 application/json; charset=utf-8315316So, GitHub returns JSON. That's great, we can use the JSON module to turn it317into Python objects. Because GitHub returned UTF-8, we should use the318``r.text`` method, not the ``r.content`` method. ``r.content`` returns a319bytestring, while ``r.text`` returns a Unicode-encoded string. I have no plans320to perform byte-manipulation on this response, so I want any Unicode code321points encoded.316So, GitHub returns JSON. That's great, we can use the ``r.json`` method to317parse it into Python objects.318319::320325 >>> import json326 >>> commit_data = json.loads(r.text)321 >>> commit_data = r.json()322 >>> print commit_data.keys()323 [u'committer', u'author', u'url', u'tree', u'sha', u'parents', u'message']324 >>> print commit_data[u'committer']325 {u'date': u'2012-05-10T11:10:50-07:00', u'email': u'me@kennethreitz.com', u'name': u'Kenneth Reitz'}
48 unmodified lines
374375 >>> r = requests.get(r.url + u'/comments')376 >>> r.status_code377 200383 >>> comments = json.loads(r.text)378 >>> comments = r.json()379 >>> print comments[0].keys()380 [u'body', u'url', u'created_at', u'updated_at', u'user', u'id']381 >>> print comments[2][u'body']382 Probably in the "advanced" section
28 unmodified lines
411 >>> auth = HTTPBasicAuth('fake@example.com', 'not_a_real_password')412 >>> r = requests.post(url=url, data=body, auth=auth)413 >>> r.status_code414 201420 >>> content = json.loads(r.text)415 >>> content = r.json()416 >>> print content[u'body']417 Sounds great! I'll get right on it.418419Brilliant. Oh, wait, no! I meant to add that it would take me a while, because
64 unmodified lines