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
How to Force a Cucumber Scenario to Fail
How to Get Out of a "Hung" State in Irb
How to List the Available Variables in an Ruby Erb Template
Getting Headers from a Ruby Net::Http Request Before Making the Request
How to Save Heroku Logs to Text File
Ruby 2.7 Says Uri.Escape Is Obsolete, What Replaces It
Scaffolding Activerecord: Two Columns of the Same Data Type
Differencebetween Ruby's 'Open-Uri' and 'Net:Http' Gems
Splitting String into Pair of Characters in Ruby
Ruby on Rails 4 - Simple_Form Multiple Select Input
Can't Run Bundle Update on Windows
Enable Dropping a File Onto a Ruby Script
All Possible Combinations of Selected Character Substitution in a String in Ruby
Time Gt Query Not Working with Mongoid and Ruby on Rails
Delete Contents of Array Based on a Set of Indexes
How to Set a Hook to Run Code at the End of a Ruby Class Definition
In Ruby, How to Get Instance Variables in a Hash Instead of an Array