如何打印有关NET:HTTPRequest的信息以进行调试? [英] How can I print information about a NET:HTTPRequest for debug purposes?

查看:230
本文介绍了如何打印有关NET:HTTPRequest的信息以进行调试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是来自Java的Ruby的新手。我正在尝试创建一个http get请求,我得到一个400的http响应代码。我通过http调用的服务非常特别,我很确定我的请求不完全正确。在我执行头部请求(下面)之后查看 req 对象是有帮助的,以便仔细检查正在发送的request_headers是我认为我的发送。有没有办法打印出req对象?

I'm new to Ruby coming from Java. I'm trying to make a http get request and I'm getting an http response code of 400. The service I'm calling over http is very particular and I'm pretty sure that my request isn't exactly correct. It'd be helpful to "look inside" the req object after I do the head request (below) to double check that the request_headers that are being sent are what I think I'm sending. Is there a way to print out the req object?

req = Net::HTTP.new(url.host, url.port)
req.use_ssl = true

res = req.head(pathWithScope, request_headers)

code = res.code.to_i
puts "Response code: #{code}"

我试过这个: puts请求调试:#{req.inspect}但它只打印这个:#< Net :: HTTP www.blah.com:443 open = false>

I tried this: puts "Request Debug: #{req.inspect}" but it only prints this: #<Net::HTTP www.blah.com:443 open=false>

推荐答案

使用 set_debug_output

http = Net::HTTP.new(url.host, url.port)
http.set_debug_output($stdout) # Logger.new("foo.log") works too

http://github.com/augustl/net-http-cheat-sheet :)

这篇关于如何打印有关NET:HTTPRequest的信息以进行调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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