2

res['location'] returns the original URL along with this error: #<Net::HTTPMovedPermanently:foo>

My code:

uri = URI.parse(url)
res = Net::HTTP.new(uri.host).request(Net::HTTP::Get.new(uri))
puts res['location']

Shouldn't res['location'] return the redirect URI?

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
six7zero9
  • 313
  • 1
  • 4
  • 15
  • That depends on what value is actually being returned by the server. What are the headers that are returned when you make the same request with e.g. `curl -I`? – Jordan Running Nov 11 '16 at 17:06
  • `"https://www.foobar.com/258182991118419351/4 ...... "` is the link. When I curl http version it redirects to the https version with a 301 – six7zero9 Nov 11 '16 at 17:16
  • The answer to your question is yes, `res['location']` should return the redirect URI. If it doesn't, it means the server isn't sending the response header you're expecting it to. You can see all of the headers it's returning with `p res.to_hash`. If you're seeing different behavior than you are with curl, there may be something different about the request you're making. You can get curl to show you its request headers using `--verbose`, and this answer shows how to do it in Ruby: http://stackoverflow.com/questions/17900207/getting-headers-from-a-ruby-nethttp-request-before-making-the-request – Jordan Running Nov 11 '16 at 17:33
  • thanks for your time. the header isn't what I was expecting ... looks like this: `{"age"=>["0"], "location"=>["https://www.foo.com/bar/baz/258182991118415722/482e914062ded88c1fc9d100d3c9b18"], "x-foo-rid"=>["247500192228"], "transfer-encoding"=>["chunked"], "date"=>["Fri, 11 Nov 2016 17:44:55 GMT"], "connection"=>["close"]}` which is the original request. When I go to the link in a browser it seems to take longer than usual to redirect. Haven't dealt with a redirect that wasn't simple. – six7zero9 Nov 11 '16 at 17:46

0 Answers0