Kong / unirest-ruby

Unirest in Ruby: Simplified, lightweight HTTP client library.
http://unirest.io/ruby
MIT License
364 stars 83 forks source link

Unirest for Ruby Build Status version

License Downloads Code Climate 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

Installing

Requirements: Ruby >= 2.0

To utilize unirest, install the unirest gem:

gem install unirest

After installing the gem package you can now begin to simplifying requests by requiring unirest:

require 'unirest'

Creating Requests

So you're probably wondering how using Unirest makes creating requests in Ruby easier, let's start with a working example:

response = Unirest.post "http://httpbin.org/post", 
                        headers:{ "Accept" => "application/json" }, 
                        parameters:{ :age => 23, :foo => "bar" }

response.code # Status code
response.headers # Response headers
response.body # Parsed body
response.raw_body # Unparsed body

Asynchronous Requests

Unirest-Ruby also supports asynchronous requests with a callback function specified inside a block, like:

response = Unirest.post "http://httpbin.org/post", 
                        headers:{ "Accept" => "application/json" }, 
                        parameters:{ :age => 23, :foo => "bar" } {|response|
    response.code # Status code
    response.headers # Response headers
    response.body # Parsed body
    response.raw_body # Unparsed body
}

File Uploads

response = Unirest.post "http://httpbin.org/post", 
                        headers:{ "Accept" => "application/json" }, 
                        parameters:{ :age => 23, :file => File.new("/path/to/file", 'rb') }

Custom Entity Body

response = Unirest.post "http://httpbin.org/post", 
                        headers:{ "Accept" => "application/json" }, 
                        parameters:{ :age => "value", :foo => "bar" }.to_json # Converting the Hash to a JSON string

Basic Authentication

Authenticating the request with basic authentication can be done by providing an auth Hash with :user and :password keys like:

response = Unirest.get "http://httpbin.org/get", auth:{:user=>"username", :password=>"password"}

Request

Unirest.get(url, headers: {}, parameters: nil, auth:nil, &callback)
Unirest.post(url, headers: {}, parameters: nil, auth:nil, &callback)
Unirest.delete(url, headers: {}, parameters: nil, auth:nil, &callback)
Unirest.put(url, headers: {}, parameters: nil, auth:nil, &callback)
Unirest.patch(url, headers: {}, parameters: nil, auth:nil, &callback)

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.

Advanced Configuration

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

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

User-Agent

The default User-Agent string is unirest-ruby/1.1. You can customize it like this:

Unirest.user_agent("custom_user_agent")

Made with ♥ from the Mashape team