How do you add a custom http header?

前端 未结 6 1047
天涯浪人
天涯浪人 2020-12-04 17:29

I\'m looking to add custom http headers to a Ruby on Rails app that is currently hosted on Heroku.

6条回答
  •  忘掉有多难
    2020-12-04 17:59

    In Rails 3 or above, simply

    headers['Header-Name'] = 'header value'
    

    works in controllers. This is even the recommended way; according to the documentation,

    Response is mostly a Ruby on Rails framework implementation detail, and should never be used directly in controllers. Controllers should use the methods defined in ActionController::Base instead. For example, if you want to set the HTTP response’s content MIME type, then use ActionController::Base#headers instead of Response#headers.

    And this is still true in Rails 6.0.

提交回复
热议问题