HTTParty parsed_response returns a string instead of Hash

The HTTParty parsed_response method returns a hash if you get a 200 response code, but otherwise it will return a string regardless of whether the web server returns an XML response.

 HTTParty.get(post_url).parsed_response.class # Depends on response code 

Amazon will provide XML (explaining what went wrong) even at 403.

Did I miss something?

+7
source share
2 answers

HTTParty parses its #parsed_response based on the HTTP Content-Type response header. Check what value is for this HTTP header. In your case, you want this to be application/xml .

+8
source

In case someone is still facing this problem today, get can take a format parameter that can help you ensure that your HTTParty::Response object is a hash:

 url = HTTParty.get('http://domain.com', format: :json) # class is a Hash 
+4
source

All Articles