em-http-requestvshttp-2
em-http-request is a Ruby gem for making asynchronous HTTP requests using EventMachine. It allows you to perform multiple requests simultaneously and handle the responses as they come in, rather than waiting for each request to complete before making the next one.
In short it supports: - Asynchronous HTTP API for single & parallel request execution - Keep-Alive and HTTP pipelining support - Auto-follow 3xx redirects with max depth - Automatic gzip & deflate decoding - Streaming response processing - Streaming file uploads - HTTP proxy and SOCKS5 support - Basic Auth & OAuth - Connection-level & global middleware support - HTTP parser via http_parser.rb - Works wherever EventMachine runs: Rubinius, JRuby, MRI
Pure Ruby, framework and transport agnostic, implementation of HTTP/2 protocol and HPACK header compression with support for:
- Binary framing parsing and encoding
- Stream multiplexing and prioritization
- Connection and stream flow control
- Header compression and server push
- Connection and stream management
- And more... see API docs
Protocol specifications:
- Hypertext Transfer Protocol Version 2 (RFC 7540)
- HPACK: Header Compression for HTTP/2 (RFC 7541)
Highlights
Example Use
EventMachine.run {
http = EventMachine::HttpRequest.new('http://google.com/').get :query => {'keyname' => 'value'}
# add callback for errors:
http.errback { p 'Uh oh'; EM.stop }
# add callback for successful requests
http.callback {
p http.response_header.status
p http.response_header
p http.response
EventMachine.stop
}
}
require 'http/2'
# GET request
client = HTTP2::Client.new
response = client.get("https://httpbin.org/get")
puts response.body
# POST reuqest
data = { name: "value" }
response = client.post("https://www.example.com", data)