HTTParty。在使用 JSON/XML 解析器解析响应之前捕获身份验证错误

发布于 2024-12-26 11:28:06 字数 1350 浏览 1 评论 0原文

使用 HTTParty 时,我遇到过无法正确捕获身份验证错误的情况,因为 JSON 或 XML 解析器拦截了“HTTP Basic:访问被拒绝”。回复。

这是我的代码:

需要'rubygems' require 'httparty'

class Client
  include HTTParty
  def initialize(host, user, password)
    self.class.base_uri host
    self.class.basic_auth user, password
  end

  def get(base_path, data_format)
    self.class.get("#{base_path}.#{data_format}")
  end
end

cl = Client.new('host.com', 'useranme', 'password')
p cl.get('/resource_path', 'json')

运行这个,如果用户名或密码错误,我会收到以下错误:

/home/ayurchuk/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/json/common.rb:148:in `parse': 743: unexpected token at 'HTTP Basic: Access denied. (MultiJson::DecodeError)
'
    from /home/ayurchuk/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/json/common.rb:148:in `parse'
    from /home/ayurchuk/.rvm/gems/ruby-1.9.3-p0@uitests/gems/multi_json-1.0.4/lib/multi_json/engines/json_common.rb:9:in `decode'
    from /home/ayurchuk/.rvm/gems/ruby-1.9.3-p0@uitests/gems/multi_json-1.0.4/lib/multi_json.rb:76:in `decode'
    from /home/ayurchuk/.rvm/gems/ruby-1.9.3-p0@uitests/gems/httparty-0.8.1/lib/httparty/parser.rb:116:in `json'
    from /home/ayurchuk/.rvm/gems/ruby-1.9.3-p0@uitests/gems/httparty-0.8.1/lib/httparty/parser.rb:136:in `parse_supported_format'
    ...

在解析器获得响应之前,有没有可能的方法来捕获这些身份验证错误?

With HTTParty I've come across a situation when I cannot correctly catch the auth error because the JSON or XML parser intercepts the "HTTP Basic: Access denied." response.

Here's my code:

require 'rubygems'
require 'httparty'

class Client
  include HTTParty
  def initialize(host, user, password)
    self.class.base_uri host
    self.class.basic_auth user, password
  end

  def get(base_path, data_format)
    self.class.get("#{base_path}.#{data_format}")
  end
end

cl = Client.new('host.com', 'useranme', 'password')
p cl.get('/resource_path', 'json')

Runnig this, if username or password is wrong, I'm getting the following error:

/home/ayurchuk/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/json/common.rb:148:in `parse': 743: unexpected token at 'HTTP Basic: Access denied. (MultiJson::DecodeError)
'
    from /home/ayurchuk/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/json/common.rb:148:in `parse'
    from /home/ayurchuk/.rvm/gems/ruby-1.9.3-p0@uitests/gems/multi_json-1.0.4/lib/multi_json/engines/json_common.rb:9:in `decode'
    from /home/ayurchuk/.rvm/gems/ruby-1.9.3-p0@uitests/gems/multi_json-1.0.4/lib/multi_json.rb:76:in `decode'
    from /home/ayurchuk/.rvm/gems/ruby-1.9.3-p0@uitests/gems/httparty-0.8.1/lib/httparty/parser.rb:116:in `json'
    from /home/ayurchuk/.rvm/gems/ruby-1.9.3-p0@uitests/gems/httparty-0.8.1/lib/httparty/parser.rb:136:in `parse_supported_format'
    ...

Is there any possible way to catch those auth errors before the parser gets the response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请叫√我孤独 2025-01-02 11:28:06

我并不是说这是最理想的解决方案,但您可以足够快地修补它。该错误来自 HTTParty 的 Parser 类,该类在 MultiJson 上调用不安全的方法,而不处理可能的错误。

我刚刚将以下内容放入初始化程序中,这使我能够检查这些响应,查找 401 状态代码,这确实是您所需要的。

module HTTParty
  class Parser
    protected
    def json
      if MultiJson.respond_to?(:adapter)
        MultiJson.load(body) rescue {}
      else
        MultiJson.decode(body) rescue {}
      end
    end
  end
end

I'm not saying this is the most desirable solution, but you could monkey patch it quickly enough. The error comes out of HTTParty's Parser class, which calls unsafe methods on MultiJson without dealing with possible errors.

I just dropped the following in an initializer, which allows me to move inspect these responses, looking for the 401 status code, which is really all you need.

module HTTParty
  class Parser
    protected
    def json
      if MultiJson.respond_to?(:adapter)
        MultiJson.load(body) rescue {}
      else
        MultiJson.decode(body) rescue {}
      end
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文