在 ruby 中访问 Net::HTTP::Post 的标头
我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要访问标头,请使用
each_header
方法:To access headers use the
each_header
method:添加:
您可以在请求之前 ,您将在控制台中看到发送到服务器的真实http请求。
对于调试这种场景非常有用。
you can add:
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.
查看 Net::HTTP 的文档
post
方法。它采用 uri 值的路径
、要发布的数据 (XML),然后是要设置的标头。它将响应和正文作为二元素数组返回。我无法对此进行测试,因为您已经掩盖了主机,并且很可能需要注册帐户,但从我使用 Net::HTTP 时的记忆来看,代码看起来是正确的。
看看 Typhoeus 作为替代方案,在我看来,它更容易使用 gem,尤其是“Making Quick”请求”部分。
Take a look at the docs for Net::HTTP's
post
method. It takes thepath
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.
Look at Typhoeus as an alternate, and, in my opinion, easier to use gem, especially the "Making Quick Requests" section.