gnip / em-http-request forked from igrigorik/em-http-request

Asynchronous HTTP Client (EventMachine + Ruby)

This URL has Read+Write access

name age message
file .autotest Fri Aug 22 10:56:57 -0700 2008 initial import [igrigorik]
file .gitignore Sun Feb 01 10:47:43 -0800 2009 Ignore build products and extconf artifacts. [careo]
file LICENSE Fri Aug 22 10:56:57 -0700 2008 initial import [igrigorik]
file README.rdoc Sun Aug 09 15:49:52 -0700 2009 refactoring :on_response to use http.stream blo... [igrigorik]
file Rakefile Thu Aug 06 20:45:15 -0700 2009 adding rake spec task [igrigorik]
file em-http-request.gemspec Loading commit data...
directory examples/ Tue May 12 20:22:34 -0700 2009 adding fibered (ruby 1.9) example for synchrono... [igrigorik]
directory ext/
directory lib/
directory test/
README.rdoc

EM-HTTP-Client

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 support
  • Custom timeouts

Screencast / Demo of using EM-HTTP-Request:

 - http://everburning.com/news/eventmachine-screencast-em-http-request/

Simple client example

  EventMachine.run {
    http = EventMachine::HttpRequest.new('http://127.0.0.1/').get :query => {'keyname' => 'value'}, :timeout => 10

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

      EventMachine.stop
    }
  }

Multi request example

  EventMachine.run {
    multi = EventMachine::MultiRequest.new

    # add multiple requests to the multi-handler
    multi.add(EventMachine::HttpRequest.new('http://www.google.com/').get)
    multi.add(EventMachine::HttpRequest.new('http://www.yahoo.com/').get)

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

      EventMachine.stop
    }
  }

Basic-Auth example

  EventMachine.run {
    http = EventMachine::HttpRequest.new('http://www.website.com/').get :head => {'authorization' => ['user', 'pass']}

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

      EventMachine.stop
    }
  }

POST example

  EventMachine.run {
    http1 = EventMachine::HttpRequest.new('http://www.website.com/').post :body => {"key1" => 1, "key2" => [2,3]}
    http2 = EventMachine::HttpRequest.new('http://www.website.com/').post :body => "some data"

    # ...
  }

Streaming body processing

  EventMachine.run {
    http = EventMachine::HttpRequest.new('http://www.website.com/').get
    http.stream { |chunk| print chunk }

    # ...
  }