从 Ruby HTTP 请求获取响应标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14719581/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:43:45  来源:igfitidea点击:

Get response headers from Ruby HTTP request

rubyhttprequest

提问by BlackHatSamurai

I'm making an HTTP request with Ruby using Net::HTTP, and I can't figure out how to get all the response headers.

我正在使用 Net::HTTP 使用 Ruby 发出 HTTP 请求,但我不知道如何获取所有响应标头。

I tried response.headerand response.headersand nothing is working.

我试过了response.header,但response.headers没有任何效果。

回答by Intrepidd

The response object actually contains the headers.

响应对象实际上包含标题。

See "Net::HTTPResponse" for more infomation.

有关更多信息,请参阅“ Net::HTTPResponse”。

You can do:

你可以做:

response['Cache-Control']

You can also call each_headeror eachon the response object to iterate through the headers.

您还可以在响应对象上调用each_headereach以遍历标头。

If you really want the headers outside of the response object, call response.to_hash

如果您真的想要响应对象之外的标头,请调用 response.to_hash

回答by Gokul M

The response Net::HTTPResponsecontains headers from Net::HTTPHeaderwhich you can get from each_headermethod as said by @Intrepidd which will return an enumerator as below:

响应Net::HTTPResponse包含标题Net::HTTPHeader,您可以从中获取each_header@Intrepidd 所说的方法,它将返回一个枚举器,如下所示:

response.each_header

#<Enumerator: #<Net::HTTPOK 200 OK readbody=true>:each_header>
[
  ["x-frame-options", "SAMEORIGIN"],
  ["x-xss-protection", "1; mode=block"],
  ["x-content-type-options", "nosniff"],
  ["content-type", "application/json; charset=utf-8"],
  ["etag", "W/\"51a4b917285f7e77dcc1a68693fcee95\""],
  ["cache-control", "max-age=0, private, must-revalidate"],
  ["x-request-id", "59943e47-5828-457d-a6da-dbac37a20729"],
  ["x-runtime", "0.162359"],
  ["connection", "close"],
  ["transfer-encoding", "chunked"]
]

You can get the actual hash using to_hmethod as below:

您可以使用to_h以下方法获取实际哈希值:

response.each_header.to_h

{
  "x-frame-options"=>"SAMEORIGIN", 
  "x-xss-protection"=>"1; mode=block", 
  "x-content-type-options"=>"nosniff", 
  "content-type"=>"application/json; charset=utf-8", 
  "etag"=>"W/\"51a4b917285f7e77dcc1a68693fcee95\"", 
  "cache-control"=>"max-age=0, private, must-revalidate", 
  "x-request-id"=>"59943e47-5828-457d-a6da-dbac37a20729", 
  "x-runtime"=>"0.162359", 
  "connection"=>"close", 
  "transfer-encoding"=>"chunked"
}

回答by Dennis

Note that the RestClientlibrary has the expected behaviour for response.headers.

请注意,该RestClient库具有response.headers.

response.headers
{
                          :server => "nginx/1.4.7",
                            :date => "Sat, 08 Nov 2014 19:44:58 GMT",
                    :content_type => "application/json",
                  :content_length => "303",
                      :connection => "keep-alive",
             :content_disposition => "inline",
     :access_control_allow_origin => "*",
          :access_control_max_age => "600",
    :access_control_allow_methods => "GET, POST, PUT, DELETE, OPTIONS",
    :access_control_allow_headers => "Content-Type, x-requested-with"
}

回答by Vlad

If you need user friendlyoutput then each_capitalizedcan be used:

如果您需要用户友好的输出,则each_capitalized可以使用:

response.each_capitalized { |key, value| puts " - #{key}: #{value}" }

This will print:

这将打印:

 - Content-Type: application/json; charset=utf-8
 - Transfer-Encoding: chunked
 - Connection: keep-alive
 - Status: 401 Unauthorized
 - Cache-Control: no-cache
 - Date: Wed, 28 Nov 2018 09:06:39 GMT

回答by Prashanth Sams

To store it in a hash =>

将其存储在哈希中 =>

response_headers = {}
your_object.response.each { |key, value|  response_headers.merge!(key.to_s => value.to_s) }

puts response_headers