| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Unirest is a set of lightweight HTTP libraries available in multiple languages.
To utilize Unirest, install it using pip:
$ pip install unirestAfter installing the pip package, you can now begin simplifying requests by importing unirest:
import unirestSo you're probably wondering how using Unirest makes creating requests in Python easier, let's start with a working example:
response = unirest.post("http://httpbin.org/post", headers={ "Accept": "application/json" }, params={ "parameter": 23, "foo": "bar" })
response.code # The HTTP status code
response.headers # The HTTP headers
response.body # The parsed response
response.raw_body # The unparsed responsePython also supports asynchronous requests in which you can define a callback function to be passed along and invoked when Unirest receives the response:
def callback_function(response):
response.code # The HTTP status code
response.headers # The HTTP headers
response.body # The parsed response
response.raw_body # The unparsed response
thread = unirest.post("http://httpbin.org/post", headers={ "Accept": "application/json" }, params={ "parameter": 23, "foo": "bar" }, callback=callback_function)Transferring file data requires that you open the file in a readable r mode:
response = unirest.post("http://httpbin.org/post", headers={"Accept": "application/json"},
params={
"parameter": "value",
"file": open("/tmp/file", mode="r")
}
)import json
response = unirest.post("http://httpbin.org/post", headers={ "Accept": "application/json" },
params=json.dumps({
"parameter": "value",
"foo": "bar"
})
)Note: For the sake of semplicity, even with custom entities in the body, the keyword argument is still params (instead of data for example). I'm looking for feedback on this.
Authenticating the request with basic authentication can be done by providing an auth array like:
response = unirest.get("http://httpbin.org/get", auth=('username', 'password'))unirest.get(url, headers = {}, params = {}, auth = (), callback = None)
unirest.post(url, headers = {}, params = {}, auth = (), callback = None)
unirest.put(url, headers = {}, params = {}, auth = (), callback = None)
unirest.patch(url, headers = {}, params = {}, auth = (), callback = None)
unirest.delete(url, headers = {}, params = {}, auth = (), callback = None)Upon receiving a response, Unirest returns the result in the form of an Object. This object should always have the same keys for each language regarding to the response details.
You can set some advanced configuration to tune Unirest-Python:
You can set a custom timeout value (in seconds):
unirest.timeout(5) # 5s timeoutYou can set default headers that will be sent on every request:
unirest.default_header('Header1','Value1')
unirest.default_header('Header2','Value2')You can clear the default headers anytime with:
unirest.clear_default_headers()Made with ♥ from the Mashape team
| Back | FazBrowse Home | New Git URL |