LoginSignup
1
0

More than 3 years have passed since last update.

Ruby で HTTP リダイレクト先の URL を取得する

Last updated at Posted at 2020-08-07

概要

  • Ruby で HTTP リダイレクト先の URL を取得する
  • 動作確認環境: Ruby 2.7.1 + macOS Catalina

ソースコード

get_redirect.rb というファイル名で以下の内容を保存する。

get_redirect.rb
require 'net/http'

# コマンドライン引数を取得
src_url = ARGV[0]

# リダイレクト先URLを取得
redirect_url = Net::HTTP.get_response(URI.parse(src_url))['location']

# リダイレクト先URLを出力
puts redirect_url if redirect_url

実行例。

$ 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

参考資料

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0