LoginSignup
0

More than 5 years have passed since last update.

Sinatra(Padrino)でredirectした時のステータスコードは2種類ある(302, 303)

Last updated at Posted at 2016-06-09

はじめに

ユーザからのリクエストに対する応答としてredirectでページ遷移させたい場合がある。Padrinoでリダイレクトした際の、コントローラから帰ってくるステータスコードをテストしていたのだが、redirectしたときのステータスコードが2種類あって困ったので調べた。

仕様としてドキュメントに明示されているかどうかはよくわからなかった。少なくともREADMEにはステータスコードについての記述は特に無いように見える。

実際の処理

Padrinoのリダイレクト処理の実体はSinatraのものを使っているようだ。以下のredirectメソッドはGithubのSinatraリポジトリより引用。

sinatra/base.rb
    # Halt processing and redirect to the URI provided.
    def redirect(uri, *args)
      if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET'
        status 303
      else
        status 302
      end

      # According to RFC 2616 section 14.30, "the field value consists of a
      # single absolute URI"
      response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?)
      halt(*args)
    end

結論

  • HTTP/1.1であり、かつMETHODがGET以外であれば303
  • 上記以外なら302

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