Last Update: Sun Sep 21 14:04:21 +0200 2008

SimpleHttp - a simplified wrapper around Net::Http

SimpleHttp aims to reduce the complexity of Net::Http while providing the most commonly used (by me) http functionality.


  • Using gem
          gem install simplehttp
  • Using +setup.rb+
          ruby setup.rb config
          ruby setup.rb install
  • tarball and zip packages are available from RubyForge


  • Require the lib:
          require 'simplehttp'
  • No fuss one line GET and POST requests:
          str = SimpleHttp.get ""
          str = SimpleHttp.get ""
  • Can use URI or String url interchangibly
          str = SimpleHttp.get URI.parse ""
  • Transparent Proxy Handling. Uses the ‘http_proxy’ environment variable if set, also provides a set_proxy method.
          http = ""
          http.set_proxy ""
 "query" => "example_query"
  • POST sends ruby Hashes as ‘application/x-www-form/urlencoded’ per default, but can send any data.
          http = ""
 imageData, "img/png"
  • Automatically handles SSL
          str = SimpleHttp.get ""
  • Easy HTTP Basic Authentication
          str = SimpleHttp.get URI.parse("")
          http = ""
          http.basic_authentication "user", "password"
 "query" => "example_query"
  • Access headers of the request or response
          http = ""
          http.request_headers["X-Custom-Header"]="useful header"
          puts "server set cookie: #{http.response_headers['set-cookie']}"
  • Automatically follows Http Redirects.

The get and post methods return a String containing the body of the request if the request was successful (HTTP 200). In case of a redirect, the redirect is followed and the ultimate response is returned. Per Default, up to three redirects are followed, this behaviour can be modified by setting follow_num_redirects.

In case of any other type of response, an exception is raised.

The default behaviour can be modified by registering handlers using the register_response_handler method. E.g. if you‘d like to retrieve the Date header instead of the body for successful transactions:

        http = ...
        http.register_response_handler(Net::HTTPSuccess) {|req,res,http|

Or you‘d like to print the Location and then raise an exception in case of a redirect:

        http = ...
        http.register_response_handler(Net::HTTPRedirect) {|req,res,http|
                puts res['location']
                raise "REDIRECT not allowed!"