httprb / http

HTTP (The Gem! a.k.a. http.rb) - a fast Ruby HTTP client with a chainable API, streaming support, and timeouts
MIT License
3.01k stars 321 forks source link

Add on_redirect callback. #722

Closed benubois closed 2 years ago

benubois commented 2 years ago

Hello,

This adds an on_redirect callback that can be passed to .follow. For example:

permanent_redirects = []
HTTP.follow(
  max_hops: 4, 
  on_redirect: proc do |from, to|
    permanent_redirects.push({ from: from.uri.to_s, to: to.uri.to_s }) if [301, 308].include?(from.status.code)
  end
).get("…")

I found this feature to be useful while trying to come up with a redirect caching system.

Thanks!