user3636388 user3636388 - 2 months ago 17
Ruby Question

How to get redirect URL withour following in RestClient

if resp.code == 302
resp.follow_redirection(req, result, &block)
else
final_url = req.url
resp.return!(req, result, &block)
final_url
end


This works to get the redirect URL. But how to get it without following redirects

Answer
RestClient.post(url, :param => p) do |response, request, result, &block|
 if [301, 302, 307].include? response.code
    redirected_url = response.headers[:location]
 else
   response.return!(request, result, &block)
 end
end