I'm looking to add custom HTTP response headers to a Ruby on Rails app that is currently hosted on Heroku.
7 Answers
Use:
response.headers['HEADER NAME'] = 'HEADER VALUE'
either in a specific method or to a before_filter method of your application controller depending on whether you need this to be added in a specific or to all of your responses.
UPDATE for Rails 5 - February 24th, 2018
As noted by @BrentMatzelle in the comments, for Rails 5:
response.set_header('HEADER NAME', 'HEADER VALUE')
4 Comments
after_action.response.set_header('HEADER NAME', 'HEADER VALUE') preferred now?In rails 5, the following solution works (in action methods)
response.set_header("Header-Name", "Header value")
Reference: edgeapi
Comments
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::Baseinstead. For example, if you want to set the HTTP response’s content MIME type, then useActionController::Base#headersinstead ofResponse#headers.
3 Comments
response object.headers method, which breaks you if you directly access the response. This is no new concept; in Java, many public getFoo() methods simply get you the private variable this.foo.In rails 4, set the response headers in the application.rb or respective environment files. Once you done that, you can override the header value wherever you required in the controller. Refer this url for more details.
Comments
If your headers are static, e.g. your own custom Server header, you can simply update config.action_dispatch.default_headers. The following example sets a custom Server header; add it to your config/application.rb or config/environments/...:
config.action_dispatch.default_headers["Server"] = "MyServer/#{config.version}"
(Assuming you set config.version earlier)
For more, see Rails Guides: Configuring Rails Applications: Configuring Action Dispatch:
config.action_dispatch.default_headersis a hash with HTTP headers that are set by default in each response.
This will be less work each request than running a controller callback.
NB: For more than one header use merge! to not remove existing essential XSS etc headers.
Comments
How to set a response header, BaseCamp style:
Use a before_action
And use a concern. Basically a glorified mixin.
class ApplicationController < ActionController::Base
include Authentication, Authorization, VersionHeaders
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern
end
# But let's look at the VersionHeaders module
# so all of that is set in a before_action
module VersionHeaders
extend ActiveSupport::Concern
included do
before_action :set_version_headers
end
private
def set_version_headers
response.headers["X-Version"] = Rails.application.config.app_version
response.headers["X-Rev"] = Rails.application.config.git_revision
end
end
## Boom - that's where the response headers are set.
## Not sure why anyone would care about the git_revision though.
## Would love someone to explain that to me.
## This is an excerpt from the WriteBook code base. Shamelessly copied without permission here. But I'm 100000% sure the base camp guys would not care, and would rather commend me for contributing to the learning of Rails / ruby.
# My thanks to them for providing me the source code from which I can learn.