Ruby-on-rails 仅打印 Rails 请求中的标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28735777/
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
Print out only headers in Rails request
提问by Victor Ronin
I know how to access a header in Rails
我知道如何访问 Rails 中的标题
request.headers["HEADER_NAME"]
However, I want to get all headers passed by a browser. I see that I can enumerate it
但是,我想获取浏览器传递的所有标头。我看到我可以枚举它
request.headers.each { |header| ... }
However, this will spit out both headers and other environment variables. Is there a way to get only headers?
但是,这会同时输出标头和其他环境变量。有没有办法只获取标题?
Update 1
更新 1
My problem isn't interation. My problem is distinguising between environment variables and headers. Both of them will be reported while interation using each or keys.
我的问题不是交互。我的问题是区分环境变量和标头。在使用每个 或 键进行交互时,它们都会被报告。
回答by Oleg Afanasyev
Solution
解决方案
By convention, headers do not usually contain dots. Nginx even rejectedrequests with dots in headers by default. So I think it's quite a safe assumption to go with.
按照惯例,标题通常不包含点。默认情况下,Nginx 甚至拒绝标头中带有点的请求。所以我认为这是一个非常安全的假设。
On contrary, all rails environment garbage is namespaced e.g. action_dispatch.show_exceptions, rack.inputetc.
相反,所有的 rails 环境垃圾都是命名空间的,例如action_dispatch.show_exceptions,rack.input等等。
These two facts conveniently suggest a way to distinguish external headers from internal variables:
这两个事实很方便地提出了一种区分外部标头和内部变量的方法:
request.headers.env.reject { |key| key.to_s.include?('.') }
Works neat.
工作整洁。
Benchmarking a bit
基准测试
Note, that include?('.')implementation works about 4 times faster than matching =~ /\./
请注意,该include?('.')实现比匹配快 4 倍=~ /\./
Benchmark.measure { 500000.times { hsh.reject { |key| key.to_s =~ /\./ } } }
=> real=2.09
Benchmark.measure { 500000.times { hsh.reject { |key| key.to_s.include?('.') } } }
=> real=0.58
Hope that helps.
希望有帮助。
回答by Patel Maimit
By using
通过使用
request.headers.each { |key, value| }
This is iterating your requested header with (key+value), but if you want specific values you have to use key name like, HTTP_KEYNAME because whenever HTTP request come it will append HTTP to keys and be sure about uppercase because it is case sensitive.
这是使用 (key+value) 迭代您请求的标头,但是如果您想要特定的值,您必须使用键名,例如 HTTP_KEYNAME,因为每当 HTTP 请求到来时,它都会将 HTTP 附加到键并确保使用大写,因为它区分大小写。
for example:
例如:
if we have passed auth_tokenas header request parameter and want to access, we can use this.
如果我们已经将auth_token作为头请求参数传递并想要访问,我们可以使用它。
request.headers["HTTP_AUTH_TOKEN"]
回答by Mayank
You can try this to get only headers list from request
您可以尝试此操作以仅从请求中获取标题列表
request.headers.first(50).to_h.keys
It will convert request.headers object into array and then to hash to get list of all keys in request to be used as
它将 request.headers 对象转换为数组,然后散列以获取请求中要用作的所有键的列表
request.headers["keyname"]
It might be not much efficient but I think it can do the job.
它可能效率不高,但我认为它可以完成这项工作。
Hope this helps.
希望这可以帮助。
回答by Carlos Roque
not sure if this is any helpful but I ended up using this brute force approach
不确定这是否有帮助,但我最终使用了这种蛮力方法
request.env.select {|k,v|
k.match("^HTTP.*|^CONTENT.*|^REMOTE.*|^REQUEST.*|^AUTHORIZATION.*|^SCRIPT.*|^SERVER.*")
}
回答by Sandip Subedi
You might be probably looking for :
您可能正在寻找:
request.env
This will basically create a Ruby Hash of the whole request object.
这将基本上创建整个请求对象的 Ruby 哈希。
For more details, check this question: How do I see the whole HTTP request in Rails
有关更多详细信息,请查看此问题: How do I see the entire HTTP request in Rails
回答by Timothy Zorn
If you just want headers:
如果你只想要标题:
request.headers.to_h.select { |k,v|
['HTTP','CONTENT','AUTHORIZATION'].any? { |s| k.to_s.starts_with? s }
}
If you want everything that's not an env var:
如果您想要不是 env var 的所有内容:
request.headers.to_h.select { |k,v|
['HTTP','CONTENT','REMOTE','REQUEST','AUTHORIZATION','SCRIPT','SERVER'].any? { |s|
k.to_s.starts_with? s
}
}
回答by Frederick Cheung
You should be able to do
你应该能够做到
request.headers.each { |key, value| }
In general when iterating over a hash ruby looks at the arity of your block and gives you either a pair (key + value) or separate variables. (The hash in this case is an object internal to the headers object)
通常,在迭代哈希 ruby 时,会查看块的数量,并为您提供一对(键 + 值)或单独的变量。(在这种情况下,哈希是标头对象内部的对象)

