Files
.com/content/posts/110219-requests.rst
Kenneth Reitz fb493bd6dd link
2012-04-26 02:24:08 -04:00

92 lines
2.8 KiB
ReStructuredText
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Requests: Python HTTP Module
############################
:date: 2011-02-19 01:22
:category: python
:featured: True
Im happy to announce the release of my latest Python module: **Requests**.
One of the most frustrating experiences I have to face, as a Python developer, is the pain of making HTTP requests. The available APIs are insane. I have to look up everything that I want to do and constantly refer to the documentation. I skip writing fun tools from time to time, simply because I dont want to go through the pain of making a simple PUT request with Basic Authentication.
Things shouldnt be this way. `Not in Python <http://www.amazon.com/gp/product/0521725968/ref=as_li_ss_tl?ie=UTF8&tag=bookforkind-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0521725968>`_.
Enter Requests
~~~~~~~~~~~~~~
::
import requests
Lets check out the awesome new Convore API. ::
>>> r = requests.get('https://convore.com/api/account/verify.json')
>>> r.status_code
401
Oops, we need to be authenticated. Lets add our credentials and try again. ::
>>> conv_auth = requests.AuthObject('requesttest', 'requesttest')
>>> r = requests.get('https://convore.com/api/account/verify.json', auth=conv_auth)
>>> r.status_code
200
It worked! Lets poke around. ::
>>> r.headers['content-type']
'application/json'
>>> r.content
'{"username": "requeststest", "url": "/users/requeststest/", "id": "9408", "img": "censored-long-url"}'
Requests is capable of much more than this, but the API stays this simple throughout.
Features
~~~~~~~~
Requests strives to be as simple yet powerful as possible. Its designed to fit the 90% use case. You might not be able to add proxies, client caching, and raw socket access, but you will have everything you need most of the time:
- Extremely simple GET, HEAD, POST, PUT, DELETE requests
- Receive simple response header dictionary, content, and status code
- Module-level HTTP Auth registry
- Eventlet/Greenlet support
The API is quite simple:
- 5 main functions: get(), head(), post(), put(), and delete()
- Attach params/data
- Add HTTP Basic Authentication with a parameter
- Add a dictionary of HTTP headers with a parameter
- Add a CookieJar with a parameter
- Attach File uploads with a parameter
Installation
~~~~~~~~~~~~
To install Requests, simply::
$ pip install requests
Or, if you must::
$ easy_install requests
But, you really shouldnt do that.
Roadmap
~~~~~~~
Requests currently supports CPython 2.5, 2.6, 2.7, and PyPy-c v1.4.
In the future, Python 3.x will be supported. There are no intentions to support Python 2.4 and older.
If youd like to help or have a feature suggestion, fork me!
[`Source on GitHub <http://github.com/kennethreitz/requests>`_]
[`PyPi Listing <http://pypi.python.org/pypi/requests>`_]