Unirest in Python: Simplified, lightweight HTTP client library.

Overview

Unirest for Python Build Status version

License Downloads Dependencies Gitter

Unirest is a set of lightweight HTTP libraries available in multiple languages, built and maintained by Mashape, who also maintain the open-source API Gateway Kong.

Features

  • Make GET, POST, PUT, PATCH, DELETE requests
  • Both syncronous and asynchronous (non-blocking) requests
  • Supports form parameters, file uploads and custom body entities
  • Supports gzip
  • Supports Basic Authentication natively
  • Customizable timeout
  • Customizable default headers for every request (DRY)
  • Automatic JSON parsing into a native object for JSON responses

Installing

To utilize Unirest, install it using pip:

$ pip install unirest

After installing the pip package, you can now begin simplifying requests by importing unirest:

import unirest

Creating Requests

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", 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 response

Asynchronous Requests

Python 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)

File Uploads

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")
  }
)

Custom Entity Body

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.

Basic Authentication

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'))

Request

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)
  • url - Endpoint, address, or URI to be acted upon and requested information from in a string format.
  • headers - Request Headers as an associative array
  • params - Request Body as an associative array or object
  • auth - The Basic Authentication credentials as an array
  • callback - Asychronous callback method to be invoked upon result.

Response

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.

  • code - HTTP Response Status Code (Example 200)
  • headers- HTTP Response Headers
  • body- Parsed response body where applicable, for example JSON responses are parsed to Objects / Associative Arrays.
  • raw_body- Un-parsed response body

Advanced Configuration

You can set some advanced configuration to tune Unirest-Python:

Timeout

You can set a custom timeout value (in seconds):

unirest.timeout(5) # 5s timeout

Default Request Headers

You 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

Comments
  • Handling of poor connection or failed connections

    Handling of poor connection or failed connections

    Hello,

    I will be honest I am not the most knowledgeable with web communication. I mostly deal with hardware communication like RS232, RS485, CAN and other serial communication often used in embedded systems. This is one thing that attached me to this library, that and simple asynchronous communication.

    My question is I am dealing with a situation with poor and intermittent wireless connections. When using unirest it appears to throw exceptions when it is unable to connect. I don't seem to be able to catch this exception in a way that I can handle it is no internet or timeout, at least not when trying to use the callback function. For now I use a basic function to connect using a normal socket and if it fails I do not try to post data with unirest.

    I know I am probably missing something important due to my lack of experience, so I am asking your thoughts on how I should handle this situation.

    I am using an embedded linux system that is single cored, which is how I found your library since it has non-blocking asynchronous calls. I am using mobile wifi as my communication path to the server.

    Any input would be helpful on how to best achieve a more robust solution.

    Thanks

    opened by TravisJoe 9
  • Submit Package to pypi

    Submit Package to pypi

    Humbly submitted:

    I would like recommend the submission of this package (directions) to pypi, the Python Package Index. This way, mashape users who want to incorporate the mashape library into a python project only have to issue

    $ pip install mashape
    

    to install and use the mashape library.

    Currently, without the package being on pypi, you have to do one of the following

    • add the mashape package source to your project
    • or, install via a somewhat-workaround of pip install git+git://github.com/Mashape/unicorn-python.git@fe922d0dbf8ded25d8cba800436013105ca203c2

    I believe adding this package to pypi would make the usage of mashape's services much easier for your users. Thanks very much for your time!

    opened by JeffPaine 4
  • Not really REST?

    Not really REST?

    Hey, guys. I like that you've made the various HTTP verbs easy to program in Python.

    However, I'm not seeing two main constraints that are required for something to be truly REST.

    1. Resources - You've handled the underlying HTTP verbs, yes. But REST is all about Resources. And I don't see anything in this library that addresses Resources. (JSON is not really an application-specific resource type, despite popular opinion.)
    2. HATEOAS - Unless they satisfy the Hypermedia constraint, web services are simply SOAP-like RPC (usually with JSON) over HTTP. Where does unirest handle hyperlinks in the Resources returned in the Response?

    I appreciate the work you're doing to standardize the HTTP Verbs across languages. However, I'd really like to have full REST support.

    opened by guitarmanvt 3
  • Set time out as deadline when using urlfetch library

    Set time out as deadline when using urlfetch library

    Currently the global time out value is not respected when using urlfetch library. This change make sure have consistency of using timeout across whichever HTTP I/O library we are using.

    Signed-off-by: Imran M Yousuf [email protected]

    opened by imyousuf 2
  • Except block for urllib2.URLError

    Except block for urllib2.URLError

    Based on visham/a271e16 (aka PR #26)

    A line got lost when reworking the try/except blocks. This line is needed to bind "response" to an appropriate object to pass to UnirestResponse.

    opened by dz111 1
  • Multiple headers with the same name

    Multiple headers with the same name

    When the response has more than one header with the same name, I have only access to the first (response.headers). Example:

    Curl header response:

    HTTP/1.1 302 Found
    Set-Cookie: PHPSESSID=lab5g40c6480s6s82r12ak6qf2; path=/; HttpOnly
    Set-Cookie: PHPSESSID=deleted; expires=Mon, 16-Feb-2015 11:13:31 GMT; path=/
    Set-Cookie: PHPSESSID=7uptb31ght23o1pi47s0un2p56; path=/; HttpOnly
    Set-Cookie: PHPSESSID=deleted; expires=Mon, 16-Feb-2015 11:13:31 GMT; path=/
    Set-Cookie: PHPSESSID=v1tu5t4ciuenm5b570jon37l63; path=/; HttpOnly
    Vary: User-Agent,Accept-Encoding
    Access-Control-Allow-Origin: *
    Content-Type: text/html; charset=UTF-8
    (...)
    

    Unirest header response

    Set-Cookie: PHPSESSID=lab5g40c6480s6s82r12ak6qf2; path=/; HttpOnly
    Vary: Accept-Encoding,User-Agent
    Access-Control-Allow-Origin: *
    Content-Type: text/html; charset=UTF-8
    (...)
    

    How to get the other headers Set-Cookie?

    opened by vmartins 1
  • Except block for urllib2.URLError

    Except block for urllib2.URLError

    It would be better if the library also handles the urllib2.URLError while making urllib2.urlopen requests. This is specially a problem if the client provides a callback because in that case things are happening on a different thread, and it's difficult for the client to catch the exception.

    The problem is that I'm not sure what the response should look like because the URLError does not have a response code or headers.

    Currently I've added something like this : _unirestResponse = UnirestResponse(0, {}, str(e.reason))

    If there's a suggestion I can modify it accordingly.

    opened by vshivam 1
  • unicode replace with \ufffd

    unicode replace with \ufffd

    Hi,

    I have noticed that using unirest python module some unicode characters from json response are replaced with \ufffd

    For example, If i call an API that returns spanish text the spanish special characters are replaced with \ufffd on json or raw text. I cant do anything on client side because server sends it that way. (this is a real response: "compa \ufffd mexicana iFone SA de CV", as you can see i cant encode it to use it in spanish because \ufffd is the unicode replacement character

    I have read that this is a Python Issue when server doesnt accept other unicode types and replaces with \ufffd

    opened by jipomx 1
  • Working example seems to be broken

    Working example seems to be broken

    (I'm using python 2.7.3) I cloned the repo, ran sudo python setup.py install and then tried the working example:

    import unirest
    response = unirest.post("http://httpbin.org/post", { "Accept": "application/json" }, { "parameter": 23, "foo": "bar" })
    

    and it failed with:

    Traceback (most recent call last): File "<stdin>", line 1, in <module> File "unirest/__init__.py", line 139, in post return __dorequest("POST", url, params, headers, callback) File "unirest/__init__.py", line 152, in __dorequest return __request(method, url, params, headers) File "unirest/__init__.py", line 66, in __request response = urllib2.urlopen(req) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/urllib2.py", line 400, in open response = self._open(req, data) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/urllib2.py", line 418, in _open '_open', req) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/urllib2.py", line 378, in _call_chain result = func(*args) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/poster/streaminghttp.py", line 142, in http_open return self.do_open(StreamingHTTPConnection, req) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/urllib2.py", line 1174, in do_open h.request(req.get_method(), req.get_selector(), req.data, headers) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/httplib.py", line 958, in request self._send_request(method, url, body, headers) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/httplib.py", line 992, in _send_request self.endheaders(body) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/httplib.py", line 954, in endheaders self._send_output(message_body) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/httplib.py", line 818, in _send_output self.send(message_body) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/poster/streaminghttp.py", line 81, in send self.sock.sendall(value) File "/usr/local/Cellar/python/2.7.3/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) TypeError: must be string or buffer, not dict

    opened by wooters 1
  • Pass extra parameters to callback

    Pass extra parameters to callback

    We should be able to pass parameters to callback function, this can be useful when we need to make multiple async requests simultaneously. For example, when we are trying to guess a password, we need to make multiple requests and if the password is correct then callback function should know the parameters of corresponding request.

    opened by asaharan 0
  • Be explicit in the relative import of utils

    Be explicit in the relative import of utils

    This avoids issues with ambiguity determining what utils you want to import if you move the unirest package around and end up with more "utils" modules in your python path.

    opened by leonelcamara 0
  • Proposing a PR to fix a few small typos

    Proposing a PR to fix a few small typos

    Issue Type

    [x] Bug (Typo)

    Steps to Replicate and Expected Behaviour

    • Examine README.md and observe syncronous, however expect to see synchronous.
    • Examine README.md and observe semplicity, however expect to see simplicity.

    Notes

    Semi-automated issue generated by https://github.com/timgates42/meticulous/blob/master/docs/NOTE.md

    To avoid wasting CI processing resources a branch with the fix has been prepared but a pull request has not yet been created. A pull request fixing the issue can be prepared from the link below, feel free to create it or request @timgates42 create the PR. Alternatively if the fix is undesired please close the issue with a small comment about the reasoning.

    https://github.com/timgates42/unirest-python/pull/new/bugfix_typos

    Thanks.

    opened by timgates42 0
  • I can Not install unirest --- can you help ? in Python

    I can Not install unirest --- can you help ? in Python

    I can Not install unirest --- can you help ? in Python PS C:\Program Files\Python37\Scripts> pip install poster Fatal error in launcher: Unable to create process using '"c:\program files\python37\python.exe" "C:\Program Files\Python37\Scripts\pip.exe" install poster' PS C:\Program Files\Python37\Scripts> pip install unirest Fatal error in launcher: Unable to create process using '"c:\program files\python37\python.exe" "C:\Program Files\Python37\Scripts\pip.exe" install unirest' PS C:\Program Files\Python37\Scripts>

    opened by lse123 3
  • Plus character + is not properly quoted

    Plus character + is not properly quoted

    I sent a request that contained a plus + character in the body and noticed that it gets replaced by a space. As you might know many web servers do this therefore the character should be quoted to %2B by unirest or data will be malformed unless you compensate for this. This is for all urlencoded data both in the query string and url encoded post body. Urllib does not have this problem in functions such as quote or urlencode

    opened by GoatSara 0
  • Url encode all strings, not just unicode

    Url encode all strings, not just unicode

    I've been having some issues with url encoding requests in an API that makes use of Unirest. It seems that params are not properly encoded if they are already in utf-8 form so characters like "+" and "," do not get escaped properly.

    opened by guitard0g 2
  • Import Error

    Import Error

    Hello,

    I am running on python 3.6 and after installing it, I run import unirest and get the following errors:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Users\EOdianosen\AppData\Local\Programs\Python\Python36-32\lib\site-packages\unirest\__init__.py", line 98
        except urllib2.HTTPError, e:
                                ^
    SyntaxError: invalid syntax
    

    Please any thing I might be missing?

    Thanks

    opened by Odianosen25 1
Owner
Kong
The Cloud Connectivity Company. Community Driven & Enterprise Adopted.
Kong
This library is for simplified work with the sms-man.com API

SMSMAN Public API Python This is a lightweight library that works as a connector to Sms-Man public API Installation pip install smsman Documentation h

null 13 Nov 19, 2022
Python module and command line script client for http://urbandictionary.com

py-urbandict py-urbandict is a client for urbandictionary.com. Project page on github: https://github.com/novel/py-urbandict PyPI: https://pypi.org/pr

Roman Bogorodskiy 32 Oct 1, 2022
un outil pour bypasser les code d'états HTTP négatif coté client ( 4xx )

4xxBypasser un outil pour bypasser les code d'états HTTP négatif coté client ( 4xx ) Liscence : MIT license Creator Installation : git clone https://g

null 21 Dec 25, 2022
Pycardano - A lightweight Cardano client in Python

PyCardano PyCardano is a standalone Cardano client written in Python. The librar

null 151 Dec 31, 2022
Zendesk Ticket Viewer is a lightweight commandline client for fetching and displaying tickets from a Zendesk account provided by the user

Zendesk Ticket Viewer is a lightweight commandline client for fetching and displaying tickets from a Zendesk account provided by the user.

Parthesh Soni 1 Jan 24, 2022
Dns-Client-Server - Dns Client Server For Python

Dns-client-server DNS Server: supporting all types of queries and replies. Shoul

Nishant Badgujar 1 Feb 15, 2022
Raphtory-client - The python client for the Raphtory project

Raphtory Client This is the python client for the Raphtory project Install via p

Raphtory 5 Apr 28, 2022
Drcom-pt-client - Drcom Pt version client with refresh timer

drcom-pt-client Drcom Pt version client with refresh timer Dr.com Pt版本客户端 可用于网页认

null 4 Nov 16, 2022
A lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.

24 July 2020 Actively soliciting contributers! Ping @ronncc if you would like to help out! pytube pytube is a very serious, lightweight, dependency-fr

pytube 7.9k Jan 2, 2023
Kevin L. 3 Jul 14, 2022
🐍 The official Python client library for Google's discovery based APIs.

Google API Client This is the Python client library for Google's discovery based APIs. To get started, please see the docs folder. These client librar

Google APIs 6.2k Jan 8, 2023
The official Python client library for the Kite Connect trading APIs

The Kite Connect API Python client - v3 The official Python client for communicating with the Kite Connect API. Kite Connect is a set of REST-like API

Zerodha Technology 756 Jan 6, 2023
🐍 The official Python client library for Google's discovery based APIs.

Google API Client This is the Python client library for Google's discovery based APIs. To get started, please see the docs folder. These client librar

Google APIs 6.2k Dec 31, 2022
Pure Python 3 MTProto API Telegram client library, for bots too!

Telethon ⭐️ Thanks everyone who has starred the project, it means a lot! Telethon is an asyncio Python 3 MTProto library to interact with Telegram's A

LonamiWebs 7.3k Jan 1, 2023
Python client library for Google Maps API Web Services

Python Client for Google Maps Services Description Use Python? Want to geocode something? Looking for directions? Maybe matrices of directions? This l

Google Maps 3.8k Jan 1, 2023
Python Client Library to interface with the Phoenix Realtime Server

supabase-realtime-client Python Client Library to interface with the Phoenix Realtime Server This is a fork of the supabase community realtime client

Anand 2 May 24, 2022
The Python client library for the Tuneup Technology App.

Tuneup Technology App Python Client Library The Python client library for the Tuneup Technology App. This library allows you to interact with the cust

Tuneup Technology 0 Jun 29, 2022
Python client library for Bigcommerce API

Bigcommerce API Python Client Wrapper over the requests library for communicating with the Bigcommerce v2 API. Install with pip install bigcommerce or

BigCommerce 81 Dec 26, 2022
Backlog API v2 Client Library for Python

BacklogPy - Backlog API v2 Client Library for Python BacklogPy is Backlog API v2 Client Library for Python 2/3 Install You can install the client libr

Koudai Aono 7 Dec 16, 2022