How to add a custom HTTP header in Rails

Set custom HTTP headers in responses of a Ruby on Rails application.
I just released stup, a tool for easily keeping organized daily notes in the terminal. You can find it on GitHub here.

If you need to add a custom HTTP header in your responses in a Ruby on Rails application, all you have to do is to add the following:

response.headers['<header name>'] = '<header value>'

either in a specific action or inside a filter of your application controller depending on whether you need this to be added in a specific or to all of your responses.


response.headers['CUSTOM'] = 'CUSTOM VALUE'

Rails 5

From Rails 5 and on, you can also use the following:

response.set_header('HEADER NAME', 'HEADER VALUE')