Reading header data in Ruby on Rails Reading header data in Ruby on Rails ruby-on-rails ruby-on-rails

Reading header data in Ruby on Rails


request.headers["Content-Type"] # => "text/plain"

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

Update for Rails 4.2

There are 2 ways to get them in Rails 4.2:Old way (still working):

request.headers["Cookie"]

New way:

request.headers["HTTP_COOKIE"]

To get a Hash with all headers of the request.

request.headers


Rails now attaches HTTP_ to the header as well as converting it to all caps so it would now be:

request.headers["HTTP_CONTENT_TYPE"]


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