LoginSignup
0
0

More than 5 years have passed since last update.

Rails勉強ネタ redirect_to でのやりとりを実際にパケットキャプチャーして見てみた

Last updated at Posted at 2018-12-09

rails.png

redirecto_toってHTTPのリダイレクトと関係あるの?
と思いwireshark(ネットワーク・アナライザ)で実際にキャプチャーしてみた。

◇wireshark
images.jpeg
↓インストール方法
https://ichibariki.hatenablog.com/entry/2017/12/10/124732

 やったこと

videos controllerがこんな感じになっていて、new_video_pathのテンプレートから
@videoをcreateするとします。
createできたらredirect_to でvideos_path(/videos つまりindex)にリダイレクトされます。

videos_controller.rb
dew index
  @videos = Video.all
end

def new
  @video = Video.new
end

def create
  @video = Video.new
  if @video.save
    redirect_to videos_path
  eles
    render :new
  end
end

結果

スクリーンショット 2018-12-09 16.12.34.png

↑SourceもDestinationもloopbackアドレス(127.0.0.1)でディスプレイフィルターしてます。
順番に

  1. リクエスト   (クライアント → サーバー)
  2. レスポンス   (クライアント ← サーバー)
  3. リクエスト   (クライアント → サーバー)
  4. レスポンス   (クライアント ← サーバー)

です。
2番目のパケットを見ると、302 Foundリダイレクトパケットを受け取っているのが分かります。

302 Foundについてはwikipedia参照
リダイレクトを受け取り、クライアントは/videosへGet(videos_path)を送信しています。

【感想】
パケットレベルでHTTPリクエストとレスポンスを覗くとイメージしやすいです。

0
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
0
0