Alex Rivera | Logout

Ruby Equivalent of Python Requests Library (HTTP Client)

Asked 2012-10-30T04:15:02.520
19

There is a library in Python that I love called "Requests". Requests is a HTTP client build on urllib3. "requests doc".

I am looking for something similar in Ruby. Basically what I need is:

  • Upload files support (multipart/form-data).
  • Easy get/post.
  • Cookies can be passed from a response object to a request object (build manually login script).
  • Stable and Flexible.
  • Sessions support (to not have to handle cookies manually if we don't have too).

I've looked at Typhoeus, but the code example in the home page doesn't work; they have moved code along and the get method is not longer directly accessible like that, so it's not starting well. Curb seems nice and I like cURL, there is also rest-client, which seems popular, and em-http seems pretty fast according to benchmark. There is a also Patron and curb-fu, which I haven't have the time to try. And, of course, Net:HTTP. But, it doesn't seem to have a mainstream solution that everyone points to.

I think a lot of people have been in my situation and I wonder what they have choosen and why?

Edit
Report

1 Answer

2

I'm sorry to hear, that Typhoeus didn't work out for you. The reason is, that the README shows howto work with Typhoeus v0.5.0.rc which can be installed with

gem install typhoeus --pre

or

gem "typhoeus", git: "git://github.com/typhoeus/typhoeus.git"

. There is no session support for Typhoeus but other than that it could be a good fit. At least its stable as hell since it is build on top of libcurl.

File sending example:

Typhoeus.post("www.example.com/file", body: { file: File.open("testfile.txt","r") })

There is unfortunately no shortcut to deal with cookies, you have to set them manually:

Typhoeus.get("www.example.com/needs_cookie", headers: { Cookie: "PRIVATE" })

TLDR: I would choose Typhoeus for its speed and libcurl if you're willing to set things up yourself. Otherwise I would look into Faraday and use it with the Typhoeus adapter.

Edit: I've added installation instructions to the README.

Edit: 0.5 is released.

answered 2012-10-30T13:35:11.803

Your Answer