简体   繁体   English

如何查看对ActiveResource请求的HTTP响应?

[英]How do I view the HTTP response to an ActiveResource request?

I am trying to debug an ActiveResource call that is not working. 我正在尝试调试无效的ActiveResource调用。

What's the best way to view the HTTP response to the request ActiveResource is making? 查看ActiveResource请求的HTTP响应的最佳方法是什么?

Monkey patch the connection to enable Net::HTTP debug mode. Monkey修补连接以启用Net :: HTTP调试模式。 See https://gist.github.com/591601 - I wrote it to solve precisely this problem. 请参阅https://gist.github.com/591601 - 我写它来解决这个问题。 Adding this gist to your rails app will give you Net::HTTP.enable_debug! 将此要点添加到您的rails应用程序将为您提供Net::HTTP.enable_debug! and Net::HTTP.disable_debug! Net::HTTP.disable_debug! that you can use to print debug info. 您可以用来打印调试信息。

Net::HTTP debug mode is insecure and shouldn't be used in production, but is extremely informative for debugging. Net :: HTTP调试模式不安全,不应在生产中使用,但对于调试非常有用。

Add a new file to config/initializers/ called 'debug_connection.rb' with the following content: 使用以下内容向config/initializers/添加一个名为'debug_connection.rb'的新文件:

class ActiveResource::Connection
  # Creates new Net::HTTP instance for communication with
  # remote service and resources.
  def http
    http = Net::HTTP.new(@site.host, @site.port)
    http.use_ssl = @site.is_a?(URI::HTTPS)
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
    http.read_timeout = @timeout if @timeout
    # Here's the addition that allows you to see the output
    http.set_debug_output $stderr
    return http
  end
end

This will print the whole network traffic to $stderr. 这将打印整个网络流量到$ stderr。

I like Wireshark because you can start it listening on the web browser client end (usually your development machine) and then do a page request. 我喜欢Wireshark,因为您可以在Web浏览器客户端(通常是您的开发机器)上启动它,然后执行页面请求。 Then you can find the HTTP packets, right click and "Follow Conversation" to see the HTTP with headers going back and forth. 然后,您可以找到HTTP数据包,右键单击“Follow Conversation”以查看带有标头来回的HTTP。

It's easy. 这很简单。 Just look at the response that comes back. 看看回来的反应。 :) :)

Two options: 两种选择:

  • You have the source file on your computer. 您的计算机上有源文件。 Edit it. 编辑它。 Put a puts response.inspect at the appropriate place. puts response.inspect适当的位置。 Remember to remove it. 记得删除它。
  • Ruby has open classes. Ruby有开放课程。 Find the right method and redefine it to do exactly what you want, or use aliases and call chaining to do this. 找到正确的方法并重新定义它以完全按照您的意愿执行,或使用别名和调用链接来执行此操作。 There's probably a method that returns the response -- grab it, print it, and then return it. 可能有一种方法可以返回响应 - 抓取它,打印它然后返回它。

Here's a silly example of the latter option. 这是后一种选择的一个愚蠢的例子。

# Somewhere buried in ActiveResource:
class Network
  def get
    return get_request
  end

  def get_request
    "I'm a request!"
  end
end

# Somewhere in your source files:
class Network
  def print_request
    request = old_get_request
    puts request
    request
  end
  alias :old_get_request :get_request
  alias :get_request :print_request
end

Imagine the first class definition is in the ActiveRecord source files. 想象一下,第一个类定义在ActiveRecord源文件中。 The second class definition is in your application somewhere. 第二个类定义在您的应用程序中。

$ irb -r openclasses.rb 
>> Network.new.get
I'm a request!
=> "I'm a request!"

You can see that it prints it and then returns it. 您可以看到它打印它然后返回它。 Neat, huh? 整洁,对吧?

(And although my simple example doesn't use it since it isn't using Rails, check out alias_method_chain to combine your alias calls.) (虽然我的简单示例不使用它,因为它不使用Rails,请查看alias_method_chain以组合您的别名调用。)

This only works if you also control the server: 这仅在您还控制服务器时有效:

Follow the server log and fish out the URL that was called: 按照服务器日志删除调用的URL:

Completed in 0.26889 (3 reqs/sec) | Rendering: 0.00036 (0%) | DB: 0.02424 (9%) | 200 OK [http://localhost/notifications/summary.xml?person_id=25738]

and then open that in Firefox. 然后在Firefox中打开它。 If the server is truely RESTful (ie. stateless) you will get the same response as ARes did. 如果服务器真的是RESTful(即无状态),您将获得与ARes相同的响应。

Maybe the best way is to use a traffic sniffer. 也许最好的方法是使用交通嗅探器。

(Which would totally work...except in my case the traffic I want to see is encrypted. D'oh!) (这将完全有效...除了在我的情况下,我想看到的流量是加密的。噢哦!)

或者,当我不知道确切的内部结构时,我进入事物的方法实际上就是抛出一个“调试器”语句,使用“script / server --debugger”启动服务器,然后逐步执行代码直到我在我想要的地方,然后在IRB开始一些检​​查.....这可能会有所帮助(嘿Luke顺便说一句)

我在这里使用TCPFlow来监视通过线路的流量,而不是修补我的应用程序输出它。

the firefox plugin live http headers ( http://livehttpheaders.mozdev.org/ ) is great for this. firefox插件live http header( http://livehttpheaders.mozdev.org/ )非常适合这个。 Or you can use a website tool like http://www.httpviewer.net/ 或者您可以使用http://www.httpviewer.net/等网站工具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM