【发布时间】:2011-12-30 08:20:26
【问题描述】:
HTTParty 的parsed_response 方法在收到响应代码 200 时返回一个 Hash,否则无论网络服务器是否返回 XML 响应,它都会返回一个字符串。
HTTParty.get(post_url).parsed_response.class # Depends on response code
即使在 403 上,亚马逊也会提供 XML(解释出了什么问题)。
我错过了什么吗?
【问题讨论】:
HTTParty 的parsed_response 方法在收到响应代码 200 时返回一个 Hash,否则无论网络服务器是否返回 XML 响应,它都会返回一个字符串。
HTTParty.get(post_url).parsed_response.class # Depends on response code
即使在 403 上,亚马逊也会提供 XML(解释出了什么问题)。
我错过了什么吗?
【问题讨论】:
HTTParty 根据 HTTP 响应的 Content-Type 标头解析其 #parsed_response。验证该 HTTP 标头的值是什么。在您的情况下,您希望它是 application/xml。
【讨论】:
Instagram 返回了 String 而不是 JSON。 content-type 显示它是“文本/html”。 example
如果今天还有人遇到这个问题,get 可以带一个格式参数,它可以帮助您确保您的 HTTParty::Response 对象是一个哈希:
url = HTTParty.get('http://domain.com', format: :json) # class is a Hash
【讨论】: