如何打印有关 NET:HTTP 请求的信息以进行调试?

发布于 2024-08-18 14:50:36 字数 524 浏览 3 评论 0原文

我是来自 Java 的 Ruby 新手。我正在尝试发出 http get 请求,但收到的 http 响应代码为 400。我通过 http 调用的服务非常特殊,我很确定我的请求不完全正确。在我执行 head 请求(如下)后,“查看内部”req 对象会很有帮助,以仔细检查正在发送的 request_headers 是否是我认为要发送的内容。有没有办法打印出 req 对象?

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 "Request Debug: #{req.inspect}" 但它只打印这个: #

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

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

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

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

发布评论

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

评论(2

花之痕靓丽 2024-08-25 14:50:36

使用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 中的内容以及更多内容:)

Use set_debug_output.

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

That and more in http://github.com/augustl/net-http-cheat-sheet :)

爱殇璃 2024-08-25 14:50:36

如果您想看&准确地调试您的应用程序发送的内容,而不仅仅是查看其日志输出,我刚刚为此发布了一个开源工具:https://httptoolkit.com/ruby/

它支持几乎所有 Ruby HTTP 库,因此它非常适合这种情况,但也适用于许多其他工具和应用程序。语言也是如此(Python、Node、Chrome、Firefox 等)。

正如另一个答案中所述,您可以配置 Net::HTTP 打印其日志以了解它正在做什么,但这只会显示它正在尝试做什么,如果您使用任何其他 HTTP 库或工具,它不会对您有帮助(或使用这样做的模块),并且它要求您更改实际的应用程序代码(并记住将其更改回来)。

使用 HTTP Toolkit,您只需单击一个按钮即可打开终端,然后像往常一样从那里运行 Ruby 代码,并且发送的每个 HTTP 请求都会自动收集。

If you want to see & debug exactly what your app is sending, not just see its log output, I've just released an open-source tool for exactly this: https://httptoolkit.com/ruby/

It supports almost all Ruby HTTP libraries so it'll work perfectly for this case, but also many other tools & languages too (Python, Node, Chrome, Firefox, etc).

As noted in the other answer you can configure Net::HTTP to print its logs to work out what it's doing, but that only shows you what it's trying to do, it won't help you if you use any other HTTP libraries or tools (or use modules that do), and it requires you to change your actual application code (and remember to change it back).

With HTTP Toolkit you can just click a button to open a terminal, run your Ruby code from there as normal, and every HTTP request sent gets collected automatically.

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