【问题标题】:Get response headers from Ruby HTTP request从 Ruby HTTP 请求中获取响应标头
【发布时间】:2013-02-06 00:35:04
【问题描述】:

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

我尝试了response.headerresponse.headers,但没有任何效果。

【问题讨论】:

  • Net/HTTP 的 API 出了名的糟糕。如果您使用的是另一个,例如 httpclient,response.header 就可以正常工作。
  • 不好是什么意思?我为什么要避免它?
  • 库本身还不错,但正如您所发现的那样,API 笨拙且不直观。我总是尽可能使用 httpclient 或具有更多功能的包装库,如 HTTParty、Rest-Client 等。
  • 感谢您的反馈!绝对是未来需要考虑的事情!再次感谢。
  • Net::HTTP 是一个相当低级的库,因此您必须编写更多代码才能完成与其他库相同的事情,但是同时,它也使您可以访问在其他库中无法执行的操作。它的 API 不错,也不笨拙,只是低级。

标签: ruby httprequest


【解决方案1】:

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

请参阅“Net::HTTPResponse”了解更多信息。

你可以这样做:

response['Cache-Control']

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

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

【讨论】:

    【解决方案2】:

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

    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"]
    ]
    

    您可以使用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"
    }
    

    【讨论】:

      【解决方案3】:

      请注意,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"
      }
      

      【讨论】:

        【解决方案4】:

        如果您需要用户友好输出,那么可以使用each_capitalized

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

        这将打印:

         - 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
        

        【讨论】:

        • 这应该是首选答案。
        【解决方案5】:

        将其存储在哈希中 =>

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

        【讨论】:

          【解决方案6】:

          这也很容易通过HTTParty 来实现:

          HTTParty.get(uri).headers
          

          【讨论】: