문제

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.

도움이 되었습니까?

해결책

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)

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top