Question

I am using an API which is returning the wrong mime type, it's coming out as text/html rather than application/json.

Some of the responses are application/json so I know that the problem is due to mime type.

But for the text/html (which returns valid json with the wrong mime type) httparty will only parse this into a string rather than a hash.

Is there a way to parse this string into a hash?

I've tried using require 'json' but using JSON.parse comes up with an unexpected key error.

Was it helpful?

Solution

If you're extending a class with HTTParty try adding

format :json

to the class

also make sure you're parsing the body of the response and not the response object.

JSON.parse(get(self.class.get("some_url","some_params").body)

OTHER TIPS

Without code or API URL I can only guess. Perhaps the API can respond in multiple ways and you need the appropriate Accept header.

class Foo
  include HTTParty
  headers 'Accept' => 'application/json'
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top