Feature #11322


OpenUri: RuntimeError: HTTP redirection loop

Added by tbsprs (Tobias Preuss) over 7 years ago. Updated about 2 years ago.

Target version:


Trying to download this file from this website with OpenUri fails with the runtime error "HTTP redirection loop".
Here is how I can reproduce the error:

> require 'open-uri'
 => true

> open('')
RuntimeError: HTTP redirection loop:
	from /home/john/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/open-uri.rb:232:in `open_loop'
	from /home/john/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/open-uri.rb:150:in `open_uri'
	from /home/john/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/open-uri.rb:716:in `open'
	from /home/john/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/open-uri.rb:34:in `open'
	from (irb):2
	from /home/john/.rvm/rubies/ruby-2.2.2/bin/irb:11:in `<main>'


open_uri-redirect-cookie-11322.patch (2.3 KB) open_uri-redirect-cookie-11322.patch jeremyevans0 (Jeremy Evans), 08/23/2019 12:42 AM

Updated by 0x0dea (D.E. Akers) over 7 years ago

The problem is not specific to OpenURI:

$ curl -L
curl: (47) Maximum (50) redirects followed

It seems the 302 Object Moved handler on this server has not been properly configured; it expects the previous request to have set a few cookies and simply sends the client back if it doesn't find them.

OpenURI appears to be incapable of handling such a circumstance, but Net::HTTP can and isn't that much more complex. I've presented below a demonstration of how you might go about orchestrating the "handshake" in order to successfully obtain the file.

conn = ''
file = '/OpenData/ShapeFiles_Zipped/'
resp = conn.get file

cookie = resp.get_fields('Set-Cookie').map { |c| c.split(';')[0] }.join(';')
resp   = conn.get file, 'Cookie' => cookie
File.write File.basename(file), resp.body

Updated by tbsprs (Tobias Preuss) over 7 years ago

Dear D.E. Akers: Your workaround works like a charm. Thank you very much.

Updated by chaimann (Eugene Chaikin) almost 7 years ago

i've had a similar issue with open('')
which i solved modifying D.E. Akers workaround a bit:

url = ''
uri = URI(url)
res = Net::HTTP.get_response(uri)
cookie = res['Set-Cookie']
req =
req['Cookie'] = cookie
res = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }

However there is another issue that i can't resolve.
Some requests don't end up in a redirection loop, but response has a status 302 Moved Temporarily and open(url) returns not the page i expect.
If I apply redirect loop workaround for this case, i get the correct page.
Tried to google but no avail so far.

Actions #4

Updated by shyouhei (Shyouhei Urabe) almost 6 years ago

  • Status changed from Open to Assigned

Updated by jeremyevans0 (Jeremy Evans) over 3 years ago

I don't think this is a bug. As 0x0dea (D.E. Akers) pointed out, other programs work the same way. However, I think cookie handling in open_uri could be a useful feature. Attached is a patch that implements the necessary support.

I used this Roda app to test the cookie redirection support:

require 'roda'

Roda.plugin :cookies
Roda.route do |r|
  r.root do
    if r.cookies['foo'] == 'bar'
      response.set_cookie('foo', 'bar')
      r.redirect '/'

run Roda

Updated by jeremyevans0 (Jeremy Evans) about 2 years ago

I've submitted the patch as a pull request, with a test:


Also available in: Atom PDF