| 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 in multiple languages.
To utilize unirest, install the unirest pip:
pip install unirest
After installing the pip package you can now begin to simplifying requests by import:
import unirest
So 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", { "Accept": "application/json" }, { "parameter": 23, "foo": "bar" })Python also has support for asynchronous requests in which you can define a callback to be passed along and invoked when Unirest recieves the response. For non-blocking requests in Python we need to define ourselves a callback to reference inside of our request method upon response:
def callback(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", { "Accept": "application/json" }, { "parameter": 23, "foo": "bar" }, callback)Transferring file data requires that you open the file in a readable r mode:
response = unirest.post("http://httpbin.org/post", {"Accept": "application/json"},
{
"parameter": "value",
"file": open("/tmp/file", mode="r")
}
)import json
response = unirest.post("http://httpbin.org/post", { "Accept": "application/json" },
json.dumps({
"parameter": "value",
"foo": "bar"
})
)unirest.get(url, headers = {}, callback = None)
unirest.post(url, headers = {}, params = {}, callback = None)
unirest.put(url, headers = {}, params = {}, callback = None)
unirest.patch(url, headers = {}, params = {}, callback = None)
unirest.delete(url, headers = {}, callback = None)Upon recieving 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.
| Back | FazBrowse Home | New Git URL |