Patron: A Simpler Ruby HTTP Client for Ruby

By Ric Roberts

Until recently, I had been using the Curb library for making HTTP requests with Ruby, and I must say I was fairly happy with it. Phillip Toland, on the other hand, wasn't satisfied with Curb's API and the fact that it is tricky to modify (being implemented mainly in C). So, he came up with an alternative called Patron, written as much as possible in Ruby.

Patron is based on libcurl, just like Curb, but it aims to provide a simpler interface whilst still taking advantage of libcurl's strengths. To use Patron you can instantiate a Session object with your desired options:

sess = Patron::Session.new sess.base_url = "http://search.twitter.com/"

...and then call methods against that session to perform your GET, HEAD, PUT, POST or DELETE operations. For example:

response = sess.get "search.json?q=ruby"

I really like the way that Patron lets you can send custom headers with your request, as I found this a bit fiddly in Curb.

sess.post("/foo/stuff", "some data", {"Content-Type" => "text/plain"})

Phillip provides more background about his design choices on his blog. You can find Patron on Github or install it as a gem.