在 ruby​​ 中访问 Net::HTTP::Post 的标头

发布于 2024-10-26 05:56:58 字数 430 浏览 2 评论 0原文

我有以下代码:

    uri = URI.parse("https://rs.xxx-travel.com/wbsapi/RequestListenerServlet")
    https = Net::HTTP.new(uri.host,uri.port)
    https.use_ssl = true
    req = Net::HTTP::Post.new(uri.path)
    req.body = searchxml
    req["Accept-Encoding"] ='gzip'
    res = https.request(req)

这通常工作正常,但另一端的服务器抱怨我的 XML 中的某些内容,并且那里的技术人员需要 xml 消息和正在发送的标头。

我已经收到 xml 消息,但我不知道如何获取与上述内容一起发送的标头。

I have the following bit of code:

    uri = URI.parse("https://rs.xxx-travel.com/wbsapi/RequestListenerServlet")
    https = Net::HTTP.new(uri.host,uri.port)
    https.use_ssl = true
    req = Net::HTTP::Post.new(uri.path)
    req.body = searchxml
    req["Accept-Encoding"] ='gzip'
    res = https.request(req)

This normally works fine but the server at the other side is complaining about something in my XML and the techies there need the xml message AND the headers that are being sent.

I've got the xml message, but I can't work out how to get at the Headers that are being sent with the above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅暮の光 2024-11-02 05:56:58

要访问标头,请使用 each_header 方法:

# Header being sent (the request object):
req.each_header do |header_name, header_value|
  puts "#{header_name} : #{header_value}"
end

# Works with the response object as well:
res.each_header do |header_name, header_value|
  puts "#{header_name} : #{header_value}"
end

To access headers use the each_header method:

# Header being sent (the request object):
req.each_header do |header_name, header_value|
  puts "#{header_name} : #{header_value}"
end

# Works with the response object as well:
res.each_header do |header_name, header_value|
  puts "#{header_name} : #{header_value}"
end
伪装你 2024-11-02 05:56:58

添加:

https.set_debug_output $stderr

您可以在请求之前 ,您将在控制台中看到发送到服务器的真实http请求。
对于调试这种场景非常有用。

you can add:

https.set_debug_output $stderr

before the request and you will see in console the real http request sent to the server.
very useful to debug this kind of scenarios.

李不 2024-11-02 05:56:58

查看 Net::HTTP 的文档 post 方法。它采用 uri 值的路径、要发布的数据 (XML),然后是要设置的标头。它将响应和正文作为二元素数组返回。

我无法对此进行测试,因为您已经掩盖了主机,并且很可能需要注册帐户,但从我使用 Net::HTTP 时的记忆来看,代码看起来是正确的。

require 'net/http'
require 'uri'

uri = URI.parse("https://rs.xxx-travel.com/wbsapi/RequestListenerServlet")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
req, body = https.post(uri.path, '<xml><blah></blah></xml>', {"Accept-Encoding" => 'gzip'})
puts "#{body.size} bytes received."
req.each{ |h,v| puts "#{h}: #{v}" }

看看 Typhoeus 作为替代方案,在我看来,它更容易使用 gem,尤其是“Making Quick”请求”部分。

Take a look at the docs for Net::HTTP's post method. It takes the path of the uri value, the data (XML) you want to post, then the headers you want to set. It returns the response and the body as a two-element array.

I can't test this because you've obscured the host, and odds are good it takes a registered account, but the code looks correct from what I remember when using Net::HTTP.

require 'net/http'
require 'uri'

uri = URI.parse("https://rs.xxx-travel.com/wbsapi/RequestListenerServlet")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
req, body = https.post(uri.path, '<xml><blah></blah></xml>', {"Accept-Encoding" => 'gzip'})
puts "#{body.size} bytes received."
req.each{ |h,v| puts "#{h}: #{v}" }

Look at Typhoeus as an alternate, and, in my opinion, easier to use gem, especially the "Making Quick Requests" section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文