The title is pretty self-explanatory. Is there any way to get the headers (except for Rack::Request.env[]
)?
标题非常明显。有没有办法获得标题(Rack :: Request.env []除外)?
2 个解决方案
#1
39
The HTTP headers are available in the Rack environment passed to your app:
传递给您的应用的Rack环境中提供了HTTP标头:
HTTP_
Variables: Variables corresponding to the client-supplied HTTP request headers (i.e., variables whose names begin with HTTP_). The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request.HTTP_变量:对应于客户端提供的HTTP请求标头的变量(即名称以HTTP_开头的变量)。这些变量的存在与否应与请求中是否存在适当的HTTP头相对应。
So the HTTP headers are prefixed with "HTTP_" and added to the hash.
因此HTTP标头以“HTTP_”为前缀并添加到散列中。
Here's a little program that extracts and displays them:
这是一个提取和显示它们的小程序:
require 'rack'
app = Proc.new do |env|
headers = env.select {|k,v| k.start_with? 'HTTP_'}
.collect {|key, val| [key.sub(/^HTTP_/, ''), val]}
.collect {|key, val| "#{key}: #{val}<br>"}
.sort
[200, {'Content-Type' => 'text/html'}, headers]
end
Rack::Server.start :app => app, :Port => 8080
When I run this, in addition to the HTTP headers as shown by Chrome or Firefox, there is a "VERSION: HTPP/1.1" (i.e. an entry with key "HTTP_VERSION" and value "HTTP/1.1" is being added to the env hash).
当我运行此操作时,除了Chrome或Firefox显示的HTTP标头之外,还有一个“VERSION:HTPP / 1.1”(即带有键“HTTP_VERSION”的条目和值“HTTP / 1.1”正在添加到环境中哈希值)。
#2
4
Based on @matt's answer, but this really gives you the request headers in a hash as requested in the question:
基于@ matt的答案,但这确实为您提供了问题中请求的哈希中的请求标头:
headers = Hash[*env.select {|k,v| k.start_with? 'HTTP_'}
.collect {|k,v| [k.sub(/^HTTP_/, ''), v]}
.collect {|k,v| [k.split('_').collect(&:capitalize).join('-'), v]}
.sort
.flatten]
Depending on what key convention you prefer you might want to use something else instead of :capitalize.
根据您喜欢的键约定,您可能希望使用其他内容而不是:capitalize。
#1
39
The HTTP headers are available in the Rack environment passed to your app:
传递给您的应用的Rack环境中提供了HTTP标头:
HTTP_
Variables: Variables corresponding to the client-supplied HTTP request headers (i.e., variables whose names begin with HTTP_). The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request.HTTP_变量:对应于客户端提供的HTTP请求标头的变量(即名称以HTTP_开头的变量)。这些变量的存在与否应与请求中是否存在适当的HTTP头相对应。
So the HTTP headers are prefixed with "HTTP_" and added to the hash.
因此HTTP标头以“HTTP_”为前缀并添加到散列中。
Here's a little program that extracts and displays them:
这是一个提取和显示它们的小程序:
require 'rack'
app = Proc.new do |env|
headers = env.select {|k,v| k.start_with? 'HTTP_'}
.collect {|key, val| [key.sub(/^HTTP_/, ''), val]}
.collect {|key, val| "#{key}: #{val}<br>"}
.sort
[200, {'Content-Type' => 'text/html'}, headers]
end
Rack::Server.start :app => app, :Port => 8080
When I run this, in addition to the HTTP headers as shown by Chrome or Firefox, there is a "VERSION: HTPP/1.1" (i.e. an entry with key "HTTP_VERSION" and value "HTTP/1.1" is being added to the env hash).
当我运行此操作时,除了Chrome或Firefox显示的HTTP标头之外,还有一个“VERSION:HTPP / 1.1”(即带有键“HTTP_VERSION”的条目和值“HTTP / 1.1”正在添加到环境中哈希值)。
#2
4
Based on @matt's answer, but this really gives you the request headers in a hash as requested in the question:
基于@ matt的答案,但这确实为您提供了问题中请求的哈希中的请求标头:
headers = Hash[*env.select {|k,v| k.start_with? 'HTTP_'}
.collect {|k,v| [k.sub(/^HTTP_/, ''), v]}
.collect {|k,v| [k.split('_').collect(&:capitalize).join('-'), v]}
.sort
.flatten]
Depending on what key convention you prefer you might want to use something else instead of :capitalize.
根据您喜欢的键约定,您可能希望使用其他内容而不是:capitalize。