How to Get the Final Url After Redirects Using Ruby

How can I get the final URL after redirects using Ruby?

Here's two ways, using both HTTPClient and Open-URI:

require 'httpclient'
require 'open-uri'

URL = 'http://www.example.org'

httpc = HTTPClient.new
resp = httpc.get(URL)
puts resp.header['Location']
>> http://www.iana.org/domains/example/

open(URL) do |resp|
puts resp.base_uri.to_s
end
>> http://www.iana.org/domains/example/

Get redirect of a URL in Ruby

You can use Net::Http and read the Location: header from the response

require 'net/http'
require 'uri'

url = URI.parse('http://www.example.com/index.html')
res = Net::HTTP.start(url.host, url.port) {|http|
http.get('/index.html')
}
res['location']

watir ruby Follow link redirection and get final url

After clicking on any link, you can get the current URL of the page by using

browser.url

in case clicking URL opening the link in new window/tab, you can use

browser.windows.last.use do
browser.url
end

Typhoeus: How to get the last redirected location?

Just discovered the this gives you the final location of the redirect:

lastUrl = response.effective_url

how to redirected using redirection url with id after create action in rails application?

Just use anchor option for url helper

redirect_to project_path(@project, anchor: 'panel2')

You need to add data-deep-link option to your tabs to store current state in the URL and allow users to open a particular tab at page load with a hash-appended URL

<ul class="tabs" data-tabs data-deep-link="true" id="example-tabs">

You can find more useful options in docs

How to get redirect URL withour following in RestClient

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


Related Topics



Leave a reply



Submit