如何检测事件资源 find()
调用返回 HTTP 206 而不是典型的 HTTP 200?
我知道 ActiveResource 会针对 HTTP 3xx-5xx 响应代码抛出各种异常,但您如何确定您收到的是什么 200 级响应代码?
最佳答案
参见 Active Resource responses, how to get them了解如何获取线程的最后响应。然后您可以根据需要测试响应代码:
class MyConn < ActiveResource::Connection
def handle_response(resp)
# Store in thread (thanks fivell for the tip).
# Use a symbol to avoid generating multiple string instances.
Thread.current[:active_resource_connection_last_response] = resp
super
end
# this is only a convenience method. You can access this directly from the current thread.
def last_resp
Thread.current[:active_resource_connection_last_response]
end
end
class MyResource < ActiveResource::Base
class << self
attr_writer :connection
end
end
myconn = MyConn.new MyResource.connection.site
MyResource.connection = myconn # replace with our enhanced version
object = MyResource.find(id)
response_code = MyResource.last_resp.code
关于ruby-on-rails - Rails ActiveResource 检测 HTTP 206 响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13348561/