Get the URL of the HTTP redirect destination in Ruby

Overview

--Get the URL of the HTTP redirect destination in Ruby --Operation check environment: Ruby 2.7.1 + macOS Catalina

Source code

Save the following contents with the file name get_redirect.rb.

get_redirect.rb


require 'net/http'

#Get command line arguments
src_url = ARGV[0]

#Get the redirect URL
redirect_url = Net::HTTP.get_response(URI.parse(src_url))['location']

#Output redirect URL
puts redirect_url if redirect_url

Execution example.

$ ruby get_redirect.rb https://bit.ly/3kmTOkc
https://t.co/yITSBp4ino
$ ruby get_redirect.rb https://t.co/yITSBp4ino
https://qiita.com/niwasawa
$ ruby get_redirect.rb https://qiita.com/niwasawa

Reference material

-library net / http \ (Ruby 2 \ .7 \ .0 reference manual ) -HTTP Redirection -HTTP \ | MDN

Recommended Posts

Get the URL of the HTTP redirect destination in Ruby
Get the URL of the HTTP redirect destination in Java
Get the result of POST in Java
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
[Rails] How to get the URL of the transition source and redirect
Get the public URL of a private Flickr file in Java
I want to get the value in Ruby
[Rails] Get the path name of the URL before the transition and change the link destination
Login fails because the redirect URL of the self-login screen is incorrect in spring-security
[Java] How to get the URL of the transition source
Count the number of occurrences of a string in Ruby
[Ruby] The role of subscripts in learning elements in arrays
Get the name of the test case in the JUnit test class
[Java] Get the file in the jar regardless of the environment
Ruby, Nokogiri: Get the element name of the selected node
Determine that the value is a multiple of 〇 in Ruby
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
About the behavior of ruby Hash # ==
Basics of sending Gmail in Ruby
[Java] Get the dates of the past Monday and Sunday in order
Implementation of ls command in Ruby
Get a proxy instance of the component itself in Spring Boot
[Ruby] See the essence of ArgumentError
[Ruby on Rails] Change the save destination of gem refile * Note
How to get the id of PRIMAY KEY auto_incremented in MyBatis
Get the ID of automatic numbering
Order of processing in the program
I want to change the value of Attribute in Selenium of Ruby
[Ruby] Display the contents of variables
How to get the length of an audio file in java
[Ruby basics] About the role of true and break in the while statement
How to constrain the action of the transition destination when not logged in
I tried to make full use of the CPU core in Ruby
How to get the absolute path of a directory running in Java
Get the value of enum saved in DB by Rails with attribute_before_type_cast
Change the save destination of the image to S3 in the Rails app. Part 2
How to get date data in Ruby
Acquisition of article information in ruby ​​scraping
Directory information of DEFAULT_CERT_FILE in Mac ruby 2.0.0
[Java] Get the day of the specific day of the week
[HTTP] Status code included in the HTTP response
The identity of params [: id] in rails
Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
part of the syntax of ruby ​​on rails
Summary of hashes and symbols in Ruby
The story of AppClip support in Anyca
[Swift] Get the height of Safe Area
[Ruby] Cut off the contents of twitter-ads
How to get the date in java
[Ruby] Classification and usage of loops in Ruby
The story of writing Java in Emacs
About the mechanism of the Web and HTTP
Write the movement of Rakefile in the runbook
Ruby: Nokogiri automatically determines the character code of html read in binary mode
[Ruby on Rails] How to make the link destination part of the specified id
Use hashes well in Ruby to calculate the total amount of an order
[Rails 6] Change redirect destination at the time of new registration / login by devise
How to get the ID of a user authenticated with Firebase in Swift
Call a method of the parent class by explicitly specifying the name in Ruby
Be careful about upgrade if you use | etc. in the URL of Tomcat
Get the path defined in Controller class of Spring boot as a list