The Ruby HTTP Gem 0.6.0: a simple, chainable HTTP request API for Ruby

The Ruby HTTP Gem provides a fast, advanced HTTP API for Ruby similar
to Python’s Requests API:

Web site: https://github.com/tarcieri/http
RubyGem: gem install http
Google Group: https://groups.google.com/forum/#!forum/ruby-http-gem

The “http” gem provides a nicer API and better performance than Ruby’s
built-in Net::HTTP. It utilizes the http_parser.rb library which
implements native extensions for HTTP response parsing on both C-based
Rubies and JRuby.

Additionally, “http” can perform multiple concurrent requests from a
single native thread utilizing Celluloid::IO, providing similar
functionality and performance to libraries like Typhoeus:
http://bit.ly/1jWjMt1

Version 0.6.0 includes multiple breaking changes with the previous API:
Response Handling

The most major difference over previous versions is how responses are
handled. Current users of the library will need to make the following
change to handle the API difference:

0.5: HTTP.get(“https://google.com”)
0.6: HTTP.get(“https://google.com”).to_s

All requests now return an HTTP::Response object instead of the
previous BodyDelegator. This provides similar convenience while still
allowing richer behaviors without convoluting the API.

New in 0.6 is also the HTTP::Response::Body object. This implements a
#readpartial method which allows the response body to be streamed
incrementally:

res = HTTP.get "http://example.com"
File.open "/tmp/dummy.bin", "wb" do |io|
  while (chunk = res.readpartial)
    io << chunk
  end
end

Header Handling

0.6 adds a new HTTP::Headers class for more abstract header handling,
most notably the ability to work with multiple values for the same
header type (e.g. Cookies). The old behavior should mostly continue to
work:

request[:content_type] = "text/plain"
request[:content_type] = "text/html"
request[:content_type] # => "text/html"

To add multiple header values, you can either use a setter with an
array:

request[:cookie] = ["foo=bar", "woo=hoo"]
request[:cookie] # => ["foo=bar", "woo=hoo"]

Or call the #add method with a key/value pair:

request.headers.add :accept, "text/plain"
request.headers.add :accept, "text/html"
request[:accept] # => ["text/plain", "text/html"]

Keepalive/Pipelining

We hoped to ship support for connection pools, keepalive, and
pipelining in this release. Due to the long time since the last
release, the number of extant API changes, and the general need to
ship a new release for bugfix purposes, we decided to postpone work on
these features for this release. It should be relatively easy to add
this sort of thing given the gem’s existing architecture, but these
are power user features that can potentially add sharp edges to the
gem’s existing API.

If you’re interested in these features, please take a look at this PR:
https://github.com/tarcieri/http/pull/72
Full Changelog

A complete list of changes is provided below. You can view discussion
about the changes at: https://github.com/tarcieri/http/issues/116

  • Rename HTTP::Request#method to HTTP::Request#verb
    (@krainboltgreene)
  • Add HTTP::ResponseBody class (@tarcieri)
  • Change API of response on HTTP::Client.request and “friends”
    (#get, #post, etc) (@tarcieri)
  • Add HTTP::Response#readpartial (@tarcieri)
  • Add HTTP::Headers class (@ixti)
  • Fix and improve following redirects (@ixti)
  • Add HTTP::Request#redirect (@ixti)
  • Add HTTP::Response#content_type (@ixti)
  • Add HTTP::Response#mime_type (@ixti)
  • Add HTTP::Response#charset (@ixti)
  • Improve error message upon invalid URI scheme (@ixti)
  • Consolidate errors under common HTTP::Error namespace (@ixti)
  • Add easy way of adding Authorization header (@ixti)
  • Fix proxy support (@hundredwatt)
  • Fix and improve query params handing (@jwinter)
  • Change API of custom MIME type parsers (@ixti)
  • Remove HTTP::Chainable#with_response (@ixti)
  • Remove HTTP::Response::BodyDelegator (@ixti)
  • Remove HTTP::Response#parsed_body (@ixti)
  • Bump up input buffer from 4K to 16K (@tarcieri)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs