How to add custom HTTP header?

I want to add custom HTTP headers to the Ruby on Rails application that is currently hosted on Heroku.

+66
ruby-on-rails
May 20 '13 at 16:32
source share
5 answers

Using:

response.headers['HEADER NAME'] = 'HEADER VALUE' 

either in a specific method or in the before_filter method of your application controller, depending on whether you need to add this in a specific or in all your answers.

+110
May 20 '13 at 16:40
source

In rails 5, the following solution works (in action methods)

 response.set_header("Header-Name", "Header value") 

Link: edgeapi

+21
Dec 24 '16 at 7:42
source

In rails 4, set the response headers in the application.rb file or corresponding environment files. After that, you can override the value of the header, wherever you are in the controller. See url for details.

+7
Sep 28 '15 at 6:16
source

In Rails 3 just

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

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

The answer is basically a detailed implementation of the Ruby on Rails implementation and should never be used directly in controllers. Controllers must use the methods defined in ActionController::Base . For example, if you want to set the MIME content type of HTTP response responses, use ActionController::Base#headers instead of Response#headers .

And this is still true in Rails 5.1 .

+6
Jun 25 '17 at 5:25
source

In rails 4 the following works:

 class API::V1::BaseController after_action :set_version_header protected def set_version_header response.headers['X-ComanyName-Api-Version'] = 'V1' end end 
+2
Feb 21 '17 at 8:17
source



All Articles