如何检查/调试/查看由RestClient生成的请求?

时间:2014-08-14 22:00:13

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

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

谢谢!

1 个答案:

答案 0 :(得分:0)

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