Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTParty parsed_response returns a String instead of Hash

Tags:

ruby

httparty

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?

like image 835
Chris Cummings Avatar asked Dec 30 '11 08:12

Chris Cummings


2 Answers

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.

like image 65
gabriel Avatar answered Nov 15 '22 23:11

gabriel


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
like image 20
morales257 Avatar answered Nov 16 '22 01:11

morales257