Chris Cummings
Chris Cummings

Reputation: 2067

HTTParty parsed_response returns a String instead of Hash

HTTParty's parsed_response method returns a Hash if you get a response code 200 but otherwise it will return a String regardless if the webserver returns a XML response.

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

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

Am I missing something?

Upvotes: 14

Views: 9851

Answers (2)

morales257
morales257

Reputation: 165

In case anyone is still encountering this issue today, get can take a format parameter, which can help you ensure your HTTParty::Response object is a Hash:

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

Upvotes: 12

gabriel
gabriel

Reputation: 1807

HTTParty parses its #parsed_response based on what the HTTP response's Content-Type header is. Verify what the value is for that HTTP header. In your case, you'd want it to be application/xml.

Upvotes: 17

Related Questions