在 Ruby on Rails 中读取头数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14729795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading header data in Ruby on Rails
提问by abhilash
I am making an API where in the access token for Facebook login will be sent in through header data.
我正在制作一个 API,其中 Facebook 登录的访问令牌将通过标头数据发送。
How do I read this data from the header?
如何从标题中读取这些数据?
回答by Eduard
request.headers["Content-Type"] # => "text/plain"
replace "Content-Type" with the name of the header that you want to read.
将“Content-Type”替换为您要阅读的标题的名称。
Update for Rails 4.2
Rails 4.2 更新
There are 2 ways to get them in Rails 4.2: Old way (still working):
在 Rails 4.2 中有 2 种方法可以获取它们:旧方法(仍在工作):
request.headers["Cookie"]
New way:
新方法:
request.headers["HTTP_COOKIE"]
To get a Hash with all headers of the request.
获取包含请求的所有标头的哈希。
request.headers
回答by cubsker
Rails now attaches HTTP_ to the header as well as converting it to all caps so it would now be:
Rails 现在将 HTTP_ 附加到标头并将其转换为所有大写字母,因此现在是:
request.headers["HTTP_CONTENT_TYPE"]
回答by BitOfUniverse
To get hash of actual httpheaders use @_headersin controller.
获取在控制器中使用的实际http标头的哈希值@_headers。

![Ruby-on-rails ActionController::RoutingError(没有路由匹配 [GET]](/res/img/loading.gif)