Reading header data in Ruby on Rails

Ruby on-RailsApiHttp Headers

Ruby on-Rails Problem Overview


I am making an API where in the access token for Facebook login will be sent in through header data.

How do I read this data from the header?

Ruby on-Rails Solutions


Solution 1 - 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

Solution 2 - Ruby on-Rails

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"]

Solution 3 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionabhilashView Question on Stackoverflow
Solution 1 - Ruby on-RailsEduardView Answer on Stackoverflow
Solution 2 - Ruby on-RailscubskerView Answer on Stackoverflow
Solution 3 - Ruby on-RailsBitOfUniverseView Answer on Stackoverflow