从 Curb 获取响应标头 [英] Get response headers from Curb

查看:57
本文介绍了从 Curb 获取响应标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算从 Ruby on Rails 应用程序进行调用:

I intend to make a call from a Ruby on Rails application:

c = Curl::Easy.http_post("https://example.com", json_string_goes_here) do |curl|
  curl.headers['Accept'] = 'application/json'
  curl.headers['Content-Type'] = 'application/json'
  curl.headers['Api-Version'] = '2.2'
end

响应应具有自定义标头:

The response should have custom headers:

X-Custom1 : "some value"
X-Custom2 : "another value"

如何遍历响应标头以将值与我期望的值进行比较?

How do I iterate over the response headers to compare the values to what I was expecting?

推荐答案

使用 Curl::Easy 的 header_str 您可以访问作为字符串返回的标题.来自文档:

Using Curl::Easy's header_str you can access the returned headers as a string. From the documentation:

返回上一次调用的响应头来执行.这是由默认的 on_header 处理程序填充的 - 如果您提供自己的标题处理程序,则此字符串将为空.

Return the response header from the previous call to perform. This is populated by the default on_header handler - if you supply your own header handler, this string will be empty.

为了测试这一点,我使用以下方法打开了内置的 Gem 服务器:

To test this I turned on the built-in Gem server using:

gem server

这里有一些代码来测试这个:

Here's some code to test this:

curl = Curl::Easy.http_get('http://0.0.0.0:8808')
curl.header_str
=> "HTTP/1.1 200 OK \r\nDate: 2013-01-10 09:07:42 -0700\r\nContent-Type: text/html\r\nServer: WEBrick/1.3.1 (Ruby/1.9.3/2012-11-10)\r\nContent-Length: 62164\r\nConnection: Keep-Alive\r\n\r\n"

捕获响应并将剩余的字符串分解为散列以使其更易于使用,很简单:

Capturing the response, and breaking the remaining string into a hash making it easier to use, is simple:

http_response, *http_headers = curl.header_str.split(/[\r\n]+/).map(&:strip)
http_headers = Hash[http_headers.flat_map{ |s| s.scan(/^(\S+): (.+)/) }]

http_response # => "HTTP/1.1 200 OK"

http_headers 
=> {
                  "Date" => "2013-01-10 09:07:42 -0700",
          "Content-Type" => "text/html",
                "Server" => "WEBrick/1.3.1 (Ruby/1.9.3/2012-11-10)",
        "Content-Length" => "62164",
            "Connection" => "Keep-Alive"
    }

再次测试,在 Pry 中:

Testing again, in Pry:

[27] (pry) main: 0> curl = Curl::Easy.http_get('http://www.example.com')
#<Curl::Easy http://www.example.com>
[28] (pry) main: 0> curl.header_str
"HTTP/1.0 302 Found\r\nLocation: http://www.iana.org/domains/example/\r\nServer: BigIP\r\nConnection: Keep-Alive\r\nContent-Length: 0\r\n\r\n"
[29] (pry) main: 0> http_response, *http_headers = curl.header_str.split(/[\r\n]+/).map(&:strip)
[
    [0] "HTTP/1.0 302 Found",
    [1] "Location: http://www.iana.org/domains/example/",
    [2] "Server: BigIP",
    [3] "Connection: Keep-Alive",
    [4] "Content-Length: 0"
]
[30] (pry) main: 0> http_headers = Hash[http_headers.flat_map{ |s| s.scan(/^(\S+): (.+)/) }]
{
          "Location" => "http://www.iana.org/domains/example/",
            "Server" => "BigIP",
        "Connection" => "Keep-Alive",
    "Content-Length" => "0"
}

这篇关于从 Curb 获取响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆