首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails ActiveResource检测HTTP206响应代码

Rails ActiveResource检测HTTP206响应代码
EN

Stack Overflow用户
提问于 2012-11-13 01:34:50
回答 1查看 439关注 0票数 3

如何检测活动资源find()调用返回的是HTTP206而不是典型的HTTP200?

我知道ActiveResource为HTTP3xx-5xx响应码抛出了各种异常,但是你怎么知道你收到的200级响应码是什么呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-13 03:07:34

有关如何获取线程的最后一个响应的信息,请参阅Active Resource responses, how to get them。然后您可以根据需要测试响应代码:

代码语言:javascript
复制
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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13348561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档