Skip to content
Dan Milne edited this page Oct 24, 2018 · 7 revisions

Automatically following redirects

The HTTP.follow option can be used for automatically following redirects:

response = HTTP.follow
               .get("https://example.com")

Limiting number of redirects

A maximum of 5 subsequent redirects will be allowed by default (anything more will raise a HTTP::Redirector::TooManyRedirectsError), but you can change the limit with the :max_hops option:

response = HTTP.follow(max_hops: 3)
               .get("https://example.com")

Unsafe redirects

If a POST, PUT or DELETE request returns a 300, 301 or 302 redirect response, it won't be followed by default and HTTP::StateError will be raised. You can set :strict to false if you want these redirects to be followed, in which case the GET verb will be used for that redirect.

response = HTTP.follow(strict: false)
               .get("https://example.com")

Final URL

The final URL can be found with the uri method

response = HTTP.follow.get("https://httpbin.org/redirect-to", params: { url: "https://httpbin.org/get" })
response.uri.to_s
=> "https://httpbin.org/get"