Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
121 lines (90 sloc) 3.48 KB


EventMachine based HTTP Request interface. Supports streaming response processing, uses Ragel HTTP parser.

  • Simple interface for single & parallel requests via deferred callbacks

  • Automatic gzip & deflate decoding

  • Basic-Auth & OAuth support

  • Custom timeouts

  • Proxy support (with SSL Tunneling)

  • Bi-directional communication with web-socket services

Screencast / Demo of using EM-HTTP-Request:

Getting started

# install & configure gemcutter repos
gem update --system
gem install gemcutter
gem tumble

gem install em-http-request

irb:0> require 'em-http'

Simple client example {
  http ='').get :query => {'keyname' => 'value'}, :timeout => 10

  http.callback {
    p http.response_header.status
    p http.response_header
    p http.response


Multi request example

Fire and wait for multiple requess to complete via the MultiRequest interface. {
  multi =

  # add multiple requests to the multi-handler

  multi.callback  {
    p multi.responses[:succeeded]
    p multi.responses[:failed]


Basic-Auth example

Full basic author support. For OAuth, check examples/oauth-tweet.rb file. {
  http ='').get :head => {'authorization' => ['user', 'pass']}

  http.errback { failed }
  http.callback {
    p http.response_header

POST example {
  http1 ='').post :body => {"key1" => 1, "key2" => [2,3]}
  http2 ='').post :body => "some data"

  # ...

Streaming body processing

Allows you to consume an HTTP stream of content in real-time. Each time a new piece of conent is pushed to the client, it is passed to the stream callback for you to operate on. {
  http ='').get { |chunk| print chunk }

  # ...

Proxy example

Full transparent proxy support with support for SSL tunneling. {
  http ='').get :proxy => {
    :host => '',
    :port => 8080,
    :authorization => ['username', 'password'] # authorization is optional

WebSocket example

Bi-directional communication with WebSockets: simply pass in a ws:// resource and the client will negotiate the connection upgrade for you. On successfull handshake the callback is invoked, and any incoming messages will be passed to the stream callback. The client can also send data to the server at will by calling the “send” method!

  • {

    http ="ws://").get :timeout => 0
    http.errback { puts "oops" }
    http.callback {
      puts "WebSocket connected!"
      http.send("Hello client")
    } { |msg|
      puts "Recieved: #{msg}"
      http.send "Pong: #{msg}"