【问题标题】:How to Inspect/Debug/View etc a request Generated by RestClient?如何检查/调试/查看等由 RestClient 生成的请求?
【发布时间】:2014-10-08 16:57:53
【问题描述】:

我有一个非常奇怪的问题,当我尝试使用 RestClient 访问 URL 时,它会超时。如果我直接从 shell 访问 URL(使用 curl)它加载正常。我可以通过 RestClient 访问其他 URL,例如 google。

此外,如果我直接使用 Net::HTTP 并发送一个 GET 请求,它会通过 (!!)。

我怀疑 RestClient 没有正确构建请求,但我不确定如何让 RestClient 注销任何有用的东西。我也不知道如何访问它生成的请求。

如何查看 RestClient 发出的完整 http 请求?

下面是一些概述问题的代码:

irb(main):002:0> RestClient.log
=> nil
irb(main):003:0> RestClient.get my_bad_uri
RestClient::RequestTimeout: Request Timeout
    from /myapp/vendor/bundle/ruby/2.1.0/gems/rest-client-1.7.2/lib/restclient/request.rb:427:in `rescue in transmit'
    from /myapp/vendor/bundle/ruby/2.1.0/gems/rest-client-1.7.2/lib/restclient/request.rb:350:in `transmit'
    from /myapp/vendor/bundle/ruby/2.1.0/gems/rest-client-1.7.2/lib/restclient/request.rb:176:in `execute'
    from /myapp/vendor/bundle/ruby/2.1.0/gems/rest-client-1.7.2/lib/restclient/request.rb:41:in `execute'
    from /myapp/vendor/bundle/ruby/2.1.0/gems/rest-client-1.7.2/lib/restclient.rb:65:in `get'
    from (irb):3
    from /myapp/vendor/bundle/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands/console.rb:47:in `start'
    from /myapp/vendor/bundle/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands/console.rb:8:in `start'
    from /myapp/vendor/bundle/ruby/2.1.0/gems/railties-3.2.18/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:33:in `require'
    from script/rails:33:in `<main>'

irb(main):004:0> RestClient.get "https://google.com" #google works
=> "<!doctype html><html itemscope=\"\" itemtype=\"http://schema.org/WebPage\" lang=\"en\"><head><meta content=\"Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.\" name=\"description\"><meta content=\"noodp\" name=\"robots\"><meta content=\"/images/google_favicon_128.png\" itemprop=\"image\"><title>Google</title><script>(function(){window.google={kEI:'WjDtU_GDMo_8oAT0woBw',kEXPI:'4791,17259,4000116,4003510,4007661,4008142,4009033,4010073,4010806,4010858,4010899,4011228,4011258,4011679,4012373,4012504,4013414,4013591,4013723,4013823,4013967,4013979,4014016,4014033,4014431,4014515,4014636,4014805,4014991,4015234,4015266,4015550,4015587,4015772,4016127,4016279,4016309,4016373,4016487,4016824,4016976,4017204,4017285,4017595,4017639,4

谢谢!

【问题讨论】:

  • 使用wireshark分析网络流量。当然,您可以使用 Ethereal,或 tcpdump,或者……好吧,您明白了。网络分析仪。其中有 很多 个。
  • @ParthianShot 由于超出此评论范围的原因,这不是我的选择:/

标签: ruby-on-rails ruby http rest-client net-http


【解决方案1】:

由于错误日志中引用了一些代码行,并且特别是救援块(/lib/restclient/request.rb:427:in 'rescue in transmit'),您可以打开 gem 并添加一些日志记录/puts/ 这些方法中的语句,以尝试找出链中挂起的位置。这不是超级外科手术,但它可以让你朝着正确的方向前进。如果您使用的是 bundler,相信您可以运行 bundle open restclient 然后深入研究 gem 的源代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    相关资源
    最近更新 更多