Reading Header Data in Ruby on Rails

I am making an API where header data will be sent in a Facebook access token.

How can I read this data from the header?

+74
api ruby-on-rails
Feb 06 '13 at
source share
3 answers
request.headers["Content-Type"] # => "text/plain" 

replace "Content-Type" with the name of the header you want to read.

Update for Rails 4.2

There are two ways to get them in Rails 4.2: The old way (still working):

 request.headers["Cookie"] 

New way:

 request.headers["HTTP_COOKIE"] 

Get a hash with all request headers.

 request.headers 
+118
Feb 06 '13 at 13:08
source share

Rails now attaches HTTP_ to the header and also converts it to all caps, so now it will:

 request.headers["HTTP_CONTENT_TYPE"] 
+14
Feb 10 '16 at 21:13
source share

To get the hash of the actual http headers, use @_headers in the controller.

-3
Jul 05 '16 at 14:47
source share



All Articles