如何在不关注 RestClient 的情况下获得重定向 URL
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
这可以获取重定向 URL。但是如何在不遵循重定向的情况下获得它
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
在rest-client 2.0中,你也可以传递max_redirects: 0
并从RestClient::MovedPermanently
或其他重定向异常对象中获取响应:
begin
RestClient::Request.execute(method: :get, url: 'http://google.com',
max_redirects: 0)
rescue RestClient::ExceptionWithResponse => err
puts err.response.inspect
if err.response.code == 302
puts err.response.headers[:location]
end
end
=> <RestClient::Response 301 "<HTML><HEAD...">
resp = RestClient.get(url) do |response, request, result, &block|
if [301, 302, 307].include? response.code
# do not redirect
response
else
response.return!(request, result, &block)
end
end
res.headers[:location]
if resp.code == 302
resp.follow_redirection(req, result, &block)
else
final_url = req.url
resp.return!(req, result, &block)
final_url
end
这可以获取重定向 URL。但是如何在不遵循重定向的情况下获得它
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
在rest-client 2.0中,你也可以传递max_redirects: 0
并从RestClient::MovedPermanently
或其他重定向异常对象中获取响应:
begin
RestClient::Request.execute(method: :get, url: 'http://google.com',
max_redirects: 0)
rescue RestClient::ExceptionWithResponse => err
puts err.response.inspect
if err.response.code == 302
puts err.response.headers[:location]
end
end
=> <RestClient::Response 301 "<HTML><HEAD...">
resp = RestClient.get(url) do |response, request, result, &block|
if [301, 302, 307].include? response.code
# do not redirect
response
else
response.return!(request, result, &block)
end
end
res.headers[:location]