Skip to content


Subversion checkout URL

You can clone with
Download ZIP

Redirects and Timeouts

igrigorik edited this page · 4 revisions

Redirects and Timeouts


em-http supports two types of timeouts: connection timeouts, and inactivity timeouts.

  • Connect timeouts (default: 5s) are invoked during the initial connection setup. If the server is slow to respond or overloaded, EventMachine will cancel the request and the errback function will be invoked.
  • Inactivity timeouts (default: 10s) are invoked after a successful connection has been made, but no data has been exchanged by either side. In cases where you have a long-lived connection (ex: stream of data from a firehose API), you may want to set this timeout to 0, which will effectively disable it. do
  http ='', :connect_timeout => 5, :inactivity_timeout => 10).get
  http.callback { }
  http.errback  { }


em-http can automatically handle 3XX redirects on your behalf. By default, the library will return the 3XX response along with the headers. However, you can provide a :redirects => depth key when you make the request to tell em-http to follow redirects up to a certain depth. do
  http ='').get :redirects => 1
  http.callback { p http.last_effective_url }

If the redirects key is provided, em-http will transparently follow all 3XX redirect codes. To check what the final URL you arrived at is, check http.last_effective_url.

Following selective redirects

Sometimes you want to restrict which redirects you follow. For example, let’s say we want to follow links to their destinations, but we want to avoid making any requests to In this case, we can setup a headers callback and inspect the headers as em-http makes the requests – anytime em-http finishes parsing the requests, this function is called. Within the headers callback, we can inspect the Location header and selective abort the connection! do
  c ='').get :redirects => 1
  c.headers do
    if c.response_header['LOCATION'] !~ /gist/
      puts "Not pointing to a gist, aborting connection"

  c.callback { puts 'done' }
  c.errback  { puts 'err' }
Something went wrong with that request. Please try again.