DEV Community

Cover image for The Top Ruby HTTP Clients for 2020
Mark Michon for Bearer

Posted on • Originally published at bearer.sh

The Top Ruby HTTP Clients for 2020

Trouble choosing an HTTP client for your Ruby project? We've got you covered. To help with the decision making, here is a list of the most popular and interesting libraries in the space.

We've also included examples of making a request in each, and some reasons each library might be of interest to you and your project.

The built-in net/http client

Before we get right into the third-party implementations, it's worth mentioning that you can make requests using Ruby's built-in net/http client from the standard library. Many of the libraries mentioned in this post use the standard HTTP client under the hood.

Perform a GET request like this:

uri = URI('https://bearer.sh')
response = Net::HTTP.get_response(uri)

puts response.code, response.body
Enter fullscreen mode Exit fullscreen mode

Perform a POST request like this:

uri = URI('https://bearer.sh')
response = Net::HTTP.post_form(uri, 'q' => 'Bearer')

puts response.code, response.body
Enter fullscreen mode Exit fullscreen mode

While net/http is built-in and convenient, it can be pretty difficult and inconsistent to work with. With many libraries acting as a more usable wrapper around net/http, it is hard to recommend starting here unless your team has deep experience with it.

Faraday

Faraday is an HTTP client library that provides a common interface over many adapters (such as Net::HTTP) and embraces the concept of Rack middleware when processing the request/response cycle.

A GET request looks like this:

response = Faraday.get("https://bearer.sh")

puts response.status, response.body
Enter fullscreen mode Exit fullscreen mode

A POST request looks like this:

response = Faraday.post("https://bearer.sh", '{"title": "Bearer Rocks!"}', "Content-Type" => "application/json")

puts response.status, response.body
Enter fullscreen mode Exit fullscreen mode

Easily the client with the nicest documentation and site design, Faraday aims to be a wrapper around common clients. The goal being to provide a shared interface. The default is the standard net/http client, but Faraday's adapter system allows it to use a variety of implementations (including some that are on this list). Like many modern Ruby HTTP clients, it also supports the ability to create connections with shared properties through the Faraday::Connection interface.

While just shy of a 1.0 release at the time of this writing, over 3600 gems depend on Faraday.

Read the Faraday docs.

http.rb (HTTP The Gem)

HTTP (The Gem! a.k.a. http.rb) is an easy-to-use client library for making requests from Ruby. It uses a simple method chaining system for building requests, similar to Python's Requests.

A GET request looks like this:

response = HTTP.get("https://bearer.sh")
puts response.status, response.body

Enter fullscreen mode Exit fullscreen mode

A POST request looks like this:

payload = {
  title: "Bearer Rocks"
}

response = HTTP.post("https://bearer.sh", json: payload)
puts response.status, response.body
Enter fullscreen mode Exit fullscreen mode

While not as popular as some of the other clients on this list, one thing that sets http.rb apart is its use of Ruby FFI bindings to bring Node.js' C-based http-parser to Ruby. This allows it to claim higher performance benchmarks than other clients that simply wrap net/http. It also includes a predictable interface with a standard array of helper methods that you see across many other libraries.

Read the http.rb docs.

rest-client

A simple HTTP and REST client for Ruby, inspired by the Sinatra's microframework style of specifying actions: get, put, post, delete.

A GET request looks like this:

response = RestClient.get('https://bearer.sh')
puts response.code, response.body

Enter fullscreen mode Exit fullscreen mode

A POST request looks like this:

response = RestClient.post('https://bearer.sh', { title: 'Bearer Rocks!'})
puts response.code, response.body
Enter fullscreen mode Exit fullscreen mode

Inspired by the Sinatra framework's style, rest-client brings a very predictable interface thanks to it's url, headers and (url, payload, headers) syntax that is shared across all the high-level helper methods (get, post, put, patch, delete). Like other clients, it too offers the ability to create named clients through it's RestClient::Resource.new interface. rest-client is one of the oldest gem on our list, dating back to 2008, but it still receives updates and is used by over 3500 other gems.

Read the rest-client docs.

httparty

Makes http fun again! Ain't no party like a httparty, because a httparty don't stop.

A GET request looks like this:

response = HTTParty.get('https://bearer.sh')
puts response.code, response.body

Enter fullscreen mode Exit fullscreen mode

A POST request looks like this:

options = {
  body: {
    title: "Bearer Rocks!"
  }
}
response = HTTPary.post('https://bearer.sh', options)
puts response.code, response.body

Enter fullscreen mode Exit fullscreen mode

httparty, fun name aside, is one of the more popular libraries on our list. It also happens to have the most gems depending on it from our list, at just shy of 4000. It includes a CLI interface, to make experimenting with endpoints and configurations easy. The documentation can be difficult to navigate for less experienced developers, but the provided examples cover most use cases.

Read the httparty docs.

excon

Excon was designed to be simple, fast and performant. It works great as a general HTTP(s) client and is particularly well suited to usage in API clients.

A GET request looks like this:

response = Excon.get('https://bearer.sh')
puts response.status, response.body
Enter fullscreen mode Exit fullscreen mode

A POST request looks like this:


response = Excon.post('https://bearer.sh', :body => '{ "title": "Bearer rocks"} ', :headers => { "Content-Type" => "application/json"})
puts response.status, response.body

Enter fullscreen mode Exit fullscreen mode

excon is another very popular HTTP client, however it isn't depended on by other gems as often as some of the other libraries we've mentioned. It includes the standard list of helper methods and the ability to create named connections through Excon.new.

Read the excon docs.

Typhoeus

Like a modern code version of the mythical beast with 100 serpent heads, Typhoeus runs HTTP requests in parallel while cleanly encapsulating handling logic.

A GET request looks like this:

response = Typhoeus.get("https://bearer.sh")
puts response.code, response.body
Enter fullscreen mode Exit fullscreen mode

A POST request looks like this:

options = {
  body: {
    title: "Bearer Rocks!"
  }
}
response = Typhoeus.post("https://bearer.sh", options)
puts response.code, response.body
Enter fullscreen mode Exit fullscreen mode

Typhoeus is interesting in that it is built on top of libcurl, and also offers the ability to make parallel requests through it's Typhoeus::Hydra interface. In includes the features you've come to expect, like helpers for commonly used actions and some additional niceties like caching and memoization. Another nice option is their custom adapter for Faraday, to mix the best of both. While somewhat popular, Typhoeus hasn't seen an update in over a year.

Read the Typhoeus docs.

Curb

Curb (probably CUrl-RuBy or something) provides Ruby-language bindings for the libcurl(3), a fully-featured client-side URL transfer library. cURL and libcurl live at http://curl.haxx.se/ .

A GET request looks like this:

response = Curl.get("https://bearer.sh")
puts response.status, response.body_str
Enter fullscreen mode Exit fullscreen mode

A POST request looks like this:

payload = {
    title: "Bearer Rocks!"
  }

response = Curl.post("https://bearer.sh", payload)
puts response.status, response.body_str
Enter fullscreen mode Exit fullscreen mode

Like Typhoeus, Curb is built on libcurl. This allows it to be very peformant. Curb offers a simple interface (Curl::Easy) for dealing with quick requests, as well as a more advanced API (Curl::Multi) for managing multiple URLs at the same time. Curb has been around since 2006, and while it may not have the popularity of some of the others on this list, it is a great option if you're looking for a gem with curl under the hood.

Use the client that's right for

You should feel confident choosing any of the popular libraries noted above. Choose the mix of feature-set and syntax that best matches the needs of your team. We also recommend keeping tabs on the state of the HTTP client gem ecosystem through Ruby Toolbox.

Did we miss your favorite client in this list? Let us know and connect @BearerSH. Looking for a solution to actively shield your application from the inconsistencies of third-party APIs? Get started with Bearer today without complex code changes or configuration.

📢 The top Ruby HTTP clients for 2020 was originally written by on the Bearer blog.

Learn more about Bearer.sh (hero image)

Top comments (0)