Giter Club home page Giter Club logo

unirest-python's Introduction

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

unirest-python's People

Contributors

dz111 avatar esseguin avatar imyousuf avatar jay754 avatar leonelcamara avatar nijikokun avatar philippbosch avatar rukku avatar shatsar avatar shimarin avatar sonicaghi avatar subnetmarco avatar vshivam avatar wooters avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unirest-python's Issues

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

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

SSL certificate validation?

Is there a way to validate a server side SSL certificate with unirest? It looks like you delegate to urllib2 to handle all of your SSL, which AFAIK, does not do so.

May want to consider porting to urllib3 (the same foundational library requests uses) in order to get better support here.

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

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

fork main process on openwrt

Hi
We faced weird issue by using unirest on openwrt,
The main process is being forked.
This happen more when the network connection is slower.

Is it possible the issue relates to unirest ?
Thanks.

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

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.

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.

Parameters property for body?

Parameters property should be called body when it is not a POJO or Query delimiter separated parameters like the other unirest libraries.

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!

You are missing an underscore

On line 82. My thing wasn't working, and then i put the underscore in and it started working. I assume that you want your things to work as well.

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>

HTTP Basic Auth - sending username but no password triggers exception

Reported on our repo here: Staffjoy/suite#3

We send an API key as an HTTP basic username without a password, e.g. curl --user API_KEY: http://app.local. This appears to throw the following error on python 2.7 with unirest 1.1.7:

Traceback (most recent call last):
  File "/opt/python/2.7.12/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/opt/python/2.7.12/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/unirest/__init__.py", line 82, in __request
    encoded_string = base64.b64encode(user + ':' + password)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Pip install error

Collecting unirest
Using cached Unirest-1.1.7.tar.gz
Collecting poster>=0.8.1 (from unirest)
Using cached poster-0.8.1.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "", line 1, in
File "/private/var/folders/pz/1xjq35vn19sfkfhtdjljm_5r0000gn/T/pip-build-44zlbnol/poster/setup.py", line 2, in
import poster
File "/private/var/folders/pz/1xjq35vn19sfkfhtdjljm_5r0000gn/T/pip-build-44zlbnol/poster/poster/init.py", line 29, in
import poster.streaminghttp
File "/private/var/folders/pz/1xjq35vn19sfkfhtdjljm_5r0000gn/T/pip-build-44zlbnol/poster/poster/streaminghttp.py", line 61
print "send:", repr(value)
^
SyntaxError: invalid syntax

----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/pz/1xjq35vn19sfkfhtdjljm_5r0000gn/T/pip-build-44zlbnol/poster/

I've tried installing from source, what gives?

Posting Django FileFields before they're saved

I'm having issues when trying to POST Django FileFields files before they're saved. I'm trying to post them during a model save override (to get extra data). So in MyModel.save override it looks like this:

class MyModel(models.Model):
    image = models.ImageField()
    def save(self, *args, **kwargs):
        res = unirest.post(url, headers=headers, params={
            "image_request[image]": self.image.file
        })
        # @todo: add some field from res.body to my model.
        super(MyModel, self).save(*args, **kwargs)

Problem is, before the model is saved, self.image.file is either an instance of InMemoryUploadedFile or TemporaryUploadedFile. When I have the latter (or after model is saved), I have to pass in open(self.image.file.path), as self.image.file.open() won't work. I wonder why.

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?

Python3 support?

In Python3.4, I run
$ pip install unirest
I get the following error:
Downloading/unpacking unirest
Downloading Unirest-1.1.6.tar.gz
Running setup.py (path:C:\Users\David\AppData\Local\Temp\pip_build_David\unirest\setup.py) egg_info for package unirest

warning: no files found matching '*.txt' under directory 'docs'

Downloading/unpacking poster>=0.8.1 (from unirest)
Downloading poster-0.8.1.tar.gz
Running setup.py (path:C:\Users\David\AppData\Local\Temp\pip_build_David\poster\setup.py) egg_info for package poster
Traceback (most recent call last):
File "", line 17, in
File "C:\Users\David\AppData\Local\Temp\pip_build_David\poster\setup.py", line 2, in
import poster
File "C:\Users\David\AppData\Local\Temp\pip_build_David\poster\poster__init__.py", line 29, in
import poster.streaminghttp
File "C:\Users\David\AppData\Local\Temp\pip_build_David\poster\poster\streaminghttp.py", line 61
print "send:", repr(value)
^
SyntaxError: invalid syntax
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File "", line 17, in

File "C:\Users\David\AppData\Local\Temp\pip_build_David\poster\setup.py", line 2, in

import poster

File "C:\Users\David\AppData\Local\Temp\pip_build_David\poster\poster__init__.py", line 29, in

import poster.streaminghttp

File "C:\Users\David\AppData\Local\Temp\pip_build_David\poster\poster\streaminghttp.py", line 61

print "send:", repr(value)

            ^

SyntaxError: invalid syntax

PS: I'm running Windows 8.1, although this looks like a problem in python2 vs python3 syntax.

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.

How do you make multiple calls from a list ?

Hello

I'm looking to loop print synonyms for each item from my list
myList = ["First", "Second", "Third"]

tried few loop methods with no success, please help

to print them manually 1 by 1 the command is

response = unirest.get("https://wordsapiv1.p.mashape.com/words/first/synonyms",
headers={
"X-Mashape-Key": "**X-Mashape-Key",
"Accept": "application/json"
}
)

response.body

this will print synonyms for word "First"

How do i loop to print body for all the words in my list ?
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.