Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add a custom http header?

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

like image 731
Jngai1297 Avatar asked May 20 '13 16:05

Jngai1297


People also ask

How do I create a HTTP header?

HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its case-insensitive name followed by a colon ( : ), then by its value. Whitespace before the value is ignored.

How do I send a custom request header?

Custom Headers are for troubleshooting, informational purposes, and specific server-side logic. For example, to send a GET request with a custom header name, you can use the "X-Real-IP" header, which defines the client's IP address. For a load balancer service, "client" is the last remote host.


1 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') 
like image 131
Lazarus Lazaridis Avatar answered Sep 28 '22 12:09

Lazarus Lazaridis