Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Patron: A Simpler Ruby HTTP Client for Ruby

By Ric Roberts / July 17, 2009

Phillip TolandUntil 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.

Comments

  1. Antonin Amand says:

    It may be a awesome implementation, but it's terrifying to see how many times the wheel gets reinveted just for the sake of a "better" api. libcurl + typhoeus + Patron ... all of those are based on libcurl and I don't talk about all the "sexy" wrappers based on net/http standard lib.
    Diversity is a good thing, but their is no point in adding new gimmicks.
    If a base library delivers maximum performance and covers the whole protocol, you can build anything upon it with ruby. People will improve it because it will improve the whole ecosystem. But to much diversity expose itself to natural selection, which mean death for a lot of projects.
    A lot of time has been spent by many developers on building THE http library. Ruby is a tremendous playground, but if we want companies to leverage the power ruby we have to stop building sex toys and start capitalizing on what's good enough to become a enterprise-grade library.

    I don't want to denigrate Philip Tolland's work. What he had done is something I wouldn't have done better. But I would like ruby to be a competitive language in a far more range than web frameworks and http clients.

  2. lolcatz says:

    Antonin Amand, +1

  3. ben says:

    I totally agreee… same thoughts; ruby's a playground that'll be killed by all those 'i wanna name my i-ve-done-it-better-but-its-pretty-much-the-same-but-its-just-a-little-bit-better work ' kind of library…

  4. Timothy Johnson says:

    The world isn't made better by denigrating someone who open-sources something he built and thought other would find useful, for philosophical reasons.

  5. Mark Murphy says:

    "Diversity is a good thing, but their is no point in adding new gimmicks."

    Open source's mantra is "scratch your own itch". Mr. Tolland had an itch. He scratched it. He made the results available as open source.

    This is one of those side-effects of living in a free society, where people are given wide latitude to do what it is they want to do.

    If you would like to live in a society where people are not granted such freedom, I am sure there are some authoritarian regimes that could use a few good engineers.

    "but if we want companies to leverage the power ruby we have to stop building sex toys and start capitalizing on what's good enough to become a enterprise-grade library."

    And somebody who has that itch to scratch will hopefully scratch it. Most likely, it is not Mr. Tolland's job to scratch your particular itch.

  6. Daniel Berger says:

    "Windows users are on your own. Good luck with that."

    Hey, thanks!

    Also, why do we need libcurl? It's ultimately just a bunch of stuff over the top of sockets, right? Can't we make it pure Ruby?

  7. Antonin Amand says:

    @timothy If you read me well I wasn't denigrating at all Mr. Tolland's work.

    @mark I understand very well what you are saying and I share your point view on open-source. I don't ask anyone to scratch my itches ;)
    It's just I would like to read about other things than http clients on blog posts.

    Maybe my language was a bit disrespectful and I apologize for that.

  8. Peter Cooper says:

    It's just as well J K Rowling didn't think there were already enough wizard stories out there.. :)

  9. Ben Marini says:

    Personally I love all the diversity, but if you read Phillip's blog you'll see that he tried patching Curb before giving in and building his own client. Sure, there are a lot of people in ruby community that want to open source a lib with a clever name, that's "simple yet powerful", and preferably has some sort of DSL. Maybe some are too quick to jump at the chance to reinvent the wheel. But what I love about the community is that it's made up of people that aren't satisfied with "good enough" solutions.

  10. alan says:

    The truth is, net/http is a square motherfucking wheel. Anyone who has ever had to use it for a couple basic tasks should know this. Sure you can write a pure ruby, simple http client, which runs too slow and eats too much mem. Ruby doesn't have its strong points in dealing with repetitive, low level tasks; so we need a balance between comfort of use and speed.

  11. Matt Aimonetti says:

    As a CouchDB Ruby DSL developer, I can tell you that I appreciate and usually test all the HTTP clients out there. Even tho they might seem pretty similar, most implementations are quite different and will fulfill different needs.

    A simple example is that only patron lets you easily reuse a connection/session by keeping it open. It might sound like a details to mainly, but in the case of CouchDB where queries are run over HTTP, this is a major advantage that improves performances by quite a lot.

    Typhoeus on the other hand has a callback mechanism and a non-blocking-multi query system.

    Anyways, if you don't like the diversity and don't need something else than net/http, then forget about what you just read.

    - Matt

  12. ben says:

    @ Timothy : not denigrating the work, but fed up with the similarity of those kind of marketable named releases. It could extend some already commonly used libraries, making those stronger and more reliable, hence would strengthen the whole ruby stuff.

    Like Anthonin can explain it, it has to become more than a sex toys playground.

    Open sourcing for open sourcing something and being able to say "hey, I'm an open sourcer" won't make ruby neither the world better.

  13. ben says:

    @ Mark Murphy : But if everibody open source its kind of the same stuff, does anything meaningful remains ? Something I don't get here.

  14. Greg says:

    I've been experimenting with Patron and compared its throughput to identical code that was using open-uri. After letting both versions run many times the Patron side was close to 2x as fast consistently.

    I appreciate that Patron is available. It won't scratch every itch I have, but for reading content from a server it's really nice to have the speed and simple interface. I'd like to see the same speed out of net/http or a similar package, because sometimes I want that low-level access, but so far I haven't had the speed be a major issue, whereas development time is always an issue. And, I'd much rather program in Ruby than go back to Perl or Java or C/C++.

  15. Jason Rogers says:

    And for those who would like a similar, easy-to-use, yet all-ruby-all-the-way approach there is RestClient (http://rest-client.heroku.com/rdoc/). Probably doesn't include libcurl's kitchen sink, but it definitely does the major verbs easily. And since it's Ruby you aren't constrained to the MRI. You can run it on the JVM (JRuby), and probably even run it in Rubinius or MagLev.

Other Posts to Enjoy

Twitter Mentions