Reading header data in Ruby on Rails

别说谁变了你拦得住时间么 提交于 2019-12-18 11:14:32

问题


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?


回答1:


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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/14729795/reading-header-data-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!