Skip to content
Myr

Merge pull request #802 from Lukasa/develop

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

Tiny formatting changes in Advanced docs

authored by Kenneth Reitz · committed by Kenneth Reitz · parents c323047, b5ce502 · Browse files · 1 file+42 added−22 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+42 added−22 removed

  • other:+42 added−22 removed

1 file+42 added−22 removed

modified:docs/user/advanced.rst+42 added-22 removed
91 unmodified lines
92 <Response [200]>9394You can also pass ``verify`` the path to a CA_BUNDLE file for private certs. You can also set the ``REQUESTS_CA_BUNDLE`` environment variable.9596Requests can also ignore verifying the SSL certficate if you set ``verify`` to False. ::96Requests can also ignore verifying the SSL certficate if you set ``verify`` to False.9798::99100 >>> requests.get('https://kennethreitz.com', verify=False)101 <Response [200]>102103By default, ``verify`` is set to True. Option ``verify`` only applies to host certs.
18 unmodified lines
122123 tarball_url = 'https://github.com/kennethreitz/requests/tarball/master'124 r = requests.get(tarball_url)125124The request has been made, but the connection is still open. The response body has not been downloaded yet. ::126The request has been made, but the connection is still open. The response body has not been downloaded yet.127128::129130 r.content131132The content has been downloaded and cached.133
148 unmodified lines
282283With ``requests.Response.iter_lines()`` you can easily iterate over streaming284APIs such as the `Twitter Streaming API <https://dev.twitter.com/docs/streaming-api>`_.285282To use the Twitter Streaming API to track the keyword "requests":286To use the Twitter Streaming API to track the keyword "requests"::287284::285288 import requests289 import json290291 r = requests.post('https://stream.twitter.com/1/statuses/filter.json',
21 unmodified lines
313Proxies314-------315316If you need to use a proxy, you can configure individual requests with the315``proxies`` argument to any request method:317``proxies`` argument to any request method::318317::318319 import requests320321 proxies = {322 "http": "10.10.1.10:3128",
11 unmodified lines
334 $ python335 >>> import requests336 >>> requests.get("http://example.org")337338To use HTTP Basic Auth with your proxy, use the `http://user:password@host/` syntax:338To use HTTP Basic Auth with your proxy, use the `http://user:password@host/` syntax::339340::341340 proxies = {341 "http": "http://user:pass@10.10.1.10:3128/",342 }343
52 unmodified lines
396into Python objects. Because GitHub returned UTF-8, we should use the397``r.text`` method, not the ``r.content`` method. ``r.content`` returns a398bytestring, while ``r.text`` returns a Unicode-encoded string. I have no plans399to perform byte-manipulation on this response, so I want any Unicode code402points encoded.::400points encoded.401402::403404 >>> import json405 >>> commit_data = json.loads(r.text)406 >>> print commit_data.keys()407 [u'committer', u'author', u'url', u'tree', u'sha', u'parents', u'message']
4 unmodified lines
412413So far, so simple. Well, let's investigate the GitHub API a little bit. Now,414we could look at the documentation, but we might have a little more fun if we415use Requests instead. We can take advantage of the Requests OPTIONS verb to416see what kinds of HTTP methods are supported on the url we just used.::416see what kinds of HTTP methods are supported on the url we just used.417418::419420 >>> verbs = requests.options(r.url)421 >>> verbs.status_code422 500423424Uh, what? That's unhelpful! Turns out GitHub, like many API providers, don't425actually implement the OPTIONS method. This is an annoying oversight, but it's426OK, we can just use the boring documentation. If GitHub had correctly427implemented OPTIONS, however, they should return the allowed methods in the426headers, e.g.::428headers, e.g.429430::431432 >>> verbs = requests.options('http://a-good-website.com/api/cats')433 >>> print verbs.headers['allow']434 GET,HEAD,POST,OPTIONS435
2 unmodified lines
438we should probably avoid making ham-handed POSTS to it. Instead, let's play439with the Issues feature of GitHub.440441This documentation was added in response to Issue #482. Given that this issue438already exists, we will use it as an example. Let's start by getting it.::442already exists, we will use it as an example. Let's start by getting it.443444::445446 >>> r = requests.get('https://api.github.com/repos/kennethreitz/requests/issues/482')447 >>> r.status_code448 200449 >>> issue = json.loads(r.text)
1 unmodified line
451 Feature any http verb in docs452 >>> print issue[u'comments']453 3454449Cool, we have three comments. Let's take a look at the last of them.::455Cool, we have three comments. Let's take a look at the last of them.456457::458459 >>> r = requests.get(r.url + u'/comments')460 >>> r.status_code461 200462 >>> comments = json.loads(r.text)
2 unmodified lines
465 >>> print comments[2][u'body']466 Probably in the "advanced" section467468Well, that seems like a silly place. Let's post a comment telling the poster461that he's silly. Who is the poster, anyway?::469that he's silly. Who is the poster, anyway?470471::472473 >>> print comments[2][u'user'][u'login']474 kennethreitz475476OK, so let's tell this Kenneth guy that we think this example should go in the477quickstart guide instead. According to the GitHub API doc, the way to do this468is to POST to the thread. Let's do it.::478is to POST to the thread. Let's do it.479480::481482 >>> body = json.dumps({u"body": u"Sounds great! I'll get right on it!"})483 >>> url = u"https://api.github.com/repos/kennethreitz/requests/issues/482/comments"484 >>> r = requests.post(url=url, data=body)485 >>> r.status_code486 404487488Huh, that's weird. We probably need to authenticate. That'll be a pain, right?489Wrong. Requests makes it easy to use many forms of authentication, including478the very common Basic Auth.::490the very common Basic Auth.491492::493494 >>> from requests.auth import HTTPBasicAuth495 >>> auth = HTTPBasicAuth('fake@example.com', 'not_a_real_password')496 >>> r = requests.post(url=url, data=body, auth=auth)497 >>> r.status_code
4 unmodified lines
502503Brilliant. Oh, wait, no! I meant to add that it would take me a while, because504I had to go feed my cat. If only I could edit this comment! Happily, GitHub505allows us to use another HTTP verb, PATCH, to edit this comment. Let's do492that.::506that.507508::509510 >>> print content[u"id"]511 5804413512 >>> body = json.dumps({u"body": u"Sounds great! I'll get right on it once I feed my cat."})513 >>> url = u"https://api.github.com/repos/kennethreitz/requests/issues/comments/5804413"
3 unmodified lines
517518Excellent. Now, just to torture this Kenneth guy, I've decided to let him519sweat and not tell him that I'm working on this. That means I want to delete520this comment. GitHub lets us delete comments using the incredibly aptly named505DELETE method. Let's get rid of it.::521DELETE method. Let's get rid of it.522523::524525 >>> r = requests.delete(url=url, auth=auth)526 >>> r.status_code527 204528 >>> r.headers['status']
1 unmodified line
530531Excellent. All gone. The last thing I want to know is how much of my ratelimit532I've used. Let's find out. GitHub sends that information in the headers, so533rather than download the whole page I'll send a HEAD request to get the516headers.::534headers.535536::537538 >>> r = requests.head(url=url, auth=auth)539 >>> print r.headers540 ...541 'x-ratelimit-remaining': '4995'
26 unmodified lines