12

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?

Chris Cummings
  • 2,007
  • 5
  • 25
  • 39

2 Answers2

14

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.

gabriel
  • 1,787
  • 2
  • 19
  • 24
  • 2
    This got me with webmock/httparty. Needed application/json content type – Andrew Jan 13 '14 at 18:40
  • @Andrew So how to get it right with Webmock? I've got no idea how to get the parsed JSON. – Nowaker Nov 13 '14 at 02:51
  • @Nowaker, I'm not sure if you sorted this out, but webmock incorrectly gives you the _exact_ code you you should stub with. It neglects, however, to include the response headers that httparty relies on to decide how to parse. Add those and it works fine. – xavdid May 25 '15 at 09:43
  • I too am getting a `String` returned instead of `JSON` from `Instagram`. `content-type` shows it's "text/html". [example](https://www.instagram.com/web/search/topsearch/?query=prawncocktail) – Rich_F Dec 27 '20 at 18:38
11

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
morales257
  • 155
  • 1
  • 11