Rack :: Request - 如何获取所有标题? [英] Rack::Request - how do I get all headers?
问题描述
标题非常明显。有没有办法得到标题(除了 Rack :: Request.env []
)?
The title is pretty self-explanatory. Is there any way to get the headers (except for Rack::Request.env[]
)?
推荐答案
HTTP标头在传递给您的应用的 Rack环境中可用:
The HTTP headers are available in the Rack environment passed to your app:
HTTP _
变量:对应于客户端提供的HTTP请求标头的变量(即,名称以HTTP_开头的变量。这些变量的存在与否应与请求中是否存在适当的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_为前缀并添加到哈希。
So the HTTP headers are prefixed with "HTTP_" and added to the hash.
这是一个提取并显示它们的小程序:
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
当我运行此功能时,除了Chrome或Firefox所示的HTTP标头之外,还有VERSION:HTPP / 1.1(即带有键HTTP_VERSION和值HTTP / 1.1的条目被添加到env哈希)。
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).
这篇关于Rack :: Request - 如何获取所有标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!