whichdigital / active-rest-client

ActiveRestClient API Client
https://rubygems.org/gems/active_rest_client
MIT License
385 stars 44 forks source link

ResponseParseException on Valid JSON #78

Closed brownmike closed 9 years ago

brownmike commented 9 years ago

I have an API that is returning valid JSON but I keep getting ActiveRestClient::ResponseParseException when it comes back.

For example, when I inspect the exception body I get this:

"{\"computer_reports\":[{\"id\":6,\"name\":\"Desktops\"},{\"id\":1,\"name\":\"Automation\"},{\"id\":2,\"name\":\"Build\"},{\"id\":3,\"name\":\"Storage\"},{\"id\":5,\"name\":\"Portables\"}]}"

Which I can then parse just fine with JSON.parse

Am I missing something here or is this a bug?

Thanks!

andyjeffries commented 9 years ago

I just tried pasting this in to one of the automated tests and it parsed just fine. However, another case where ActiveRestClient::ResponseParseException may be raised is if the API doesn't return a Content-Type HTTP header containing at least "json". Does your API send back that header?

If not, you should be able to use an after_request callback to add it in.

andyjeffries commented 9 years ago

I'm going to close this issue for now (as I can't reproduce it). Feel free to comment back if it's not working and I'll reopen it. If it's still not, can you please paste the entirety of your HTTP API response (including all headers) and I'll try to reproduce it using your response.

brownmike commented 9 years ago

Ah, you're exactly right about the response header. It was sending back Content-Type: text/plain;charset=UTF-8

Thanks for the response!