我正在尝试调试一个无法工作的ActiveResource调用。
查看ActiveResource发出的请求的HTTP响应的最佳方式是什么?
发布于 2008-10-25 13:09:11
很简单。只需看一下返回的响应。:)
有两个选项:
puts response.inspect。记住删除它。下面是后一种选择的一个愚蠢的例子。
# 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来组合您的别名调用。)
发布于 2011-08-24 20:30:03
Monkey修补连接以启用Net::HTTP调试模式。参见https://gist.github.com/591601 -我写它就是为了解决这个问题。将这个要点添加到您的rails应用程序将会给您提供Net::HTTP.enable_debug!和Net::HTTP.disable_debug!,您可以使用它们来打印调试信息。
HTTP调试模式是不安全的,不应该在生产中使用,但它对调试非常有用。
发布于 2011-11-08 02:25:43
在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。
https://stackoverflow.com/questions/227907
复制相似问题