如果您获得响应代码 200, HTTParty 的parsed_response
方法将返回一个 Hash,否则无论网络服务器是否返回 XML 响应,它都会返回一个字符串。
HTTParty.get(post_url).parsed_response.class # Depends on response code
即使在 403 上,亚马逊也会提供 XML(解释出了什么问题)。
我错过了什么吗?
HTTParty#parsed_response
根据 HTTP 响应的Content-Type
标头对其进行解析。验证该 HTTP 标头的值是什么。在您的情况下,您希望它是application/xml
.
如果今天还有人遇到这个问题,get
可以带一个格式参数,它可以帮助你确保你的HTTParty::Response
对象是一个哈希:
url = HTTParty.get('http://domain.com', format: :json) # class is a Hash