-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle "null" json responses #103
Conversation
When a json api responds with a "null" body and status 200, parsing did fail. With this patch it returns nil as response.
I think I'd rather it returned an empty object of the current class rather than nil, that way you can still check things like the return status or have callbacks extract standard custom headers, etc. |
Ok. This would be fine too. |
Would you like to resubmit a PR with that response, or close this one and open a new one, or close this one and I'll do it when I get a chance ;-) ? |
"null" json responses are now handled and return an empty object.
Wow, that was quick! |
Many thanks for this gem 👍. Hope that this PR is ok. |
Actually, is there a test for this? The existing begin
if @response.body.blank? || @response.body == "null"
body = {}
else
body = MultiJson.load(@response.body)
end
rescue MultiJson::ParseError
raise ResponseParseException.new(status:@response.status, body:@response.body, headers:@response.headers)
end |
And you're very welcome for the gem, glad you like it :-) |
This also would be an option. I thought it would be more safe to let the json parser decide what a null value actually is. But the string comparison may also be an option. I'll change it. |
Ahhhh OK, ignore me - I thought the JSON parser was failing on the |
Great! Many thanks! |
Released as v1.7.2 |
Perfect. That was also fast :). |
"null" gets parsed to nil and causes the following code to break.
With this patch nil gets returned.