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

问题描述 投票:0回答:9

我正在尝试调试不起作用的 ActiveResource 调用。

查看 ActiveResource 发出的请求的 HTTP 响应的最佳方式是什么?

ruby-on-rails ruby activeresource
9个回答
14
投票

Monkey 修补连接以启用 Net::HTTP 调试模式。请参阅 https://gist.github.com/591601 - 我编写它就是为了解决这个问题。将此要点添加到您的 Rails 应用程序将为您提供

Net::HTTP.enable_debug!
Net::HTTP.disable_debug!
,您可以使用它们来打印调试信息。

Net::HTTP 调试模式不安全,不应在生产中使用,但对于调试来说信息量极大。


4
投票

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

这会将整个网络流量打印到 $stderr。


3
投票

这很容易。只需查看返回的响应即可。 :)

两种选择:

  • 您的计算机上有源文件。编辑它。将
    puts response.inspect
    放在适当的位置。记得把它删除。
  • Ruby有公开课。找到正确的方法并重新定义它以完全执行您想要的操作,或者使用别名和调用链来执行此操作。可能有一个方法可以返回响应——抓取它,打印它,然后返回它。

这是后一个选项的一个愚蠢的例子。

# 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

想象第一个类定义位于 ActiveRecord 源文件中。第二类定义位于您的应用程序中的某个位置。

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

你可以看到它打印了它然后返回它。整洁吧?

(虽然我的简单示例没有使用它,因为它没有使用 Rails,但请查看

alias_method_chain
来组合您的别名调用。)


3
投票

我喜欢Wireshark,因为你可以启动它在网络浏览器客户端(通常是你的开发机器)上监听,然后执行页面请求。然后你可以找到 HTTP 数据包,右键单击“Follow Conversation”来查看带有来回标题的 HTTP。


1
投票

这仅在您也控制服务器时才有效:

按照服务器日志找出被调用的 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]

然后在 Firefox 中打开它。如果服务器真正是 RESTful(即无状态),您将得到与 ARes 相同的响应。


0
投票

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


0
投票

也许最好的方法是使用流量嗅探器。

(这完全可以工作......除了在我的情况下,我想看到的流量是加密的。哦!)


0
投票

[`我已经从堆栈溢出中删除了所有答案,因为我不想为提取性、剥削性、“人工智能”服务的培训做出贡献。抱歉。]


-1
投票
firefox 插件 live http headers (

http://livehttpheaders.mozdev.org/) 非常适合这个。或者您可以使用网站工具,例如 http://www.httpviewer.net/

© www.soinside.com 2019 - 2024. All rights reserved.