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
Allow custom json body #289
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @jeremy (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
lib/active_resource/connection.rb
Outdated
@@ -144,6 +150,14 @@ def handle_response(response) | |||
when 301, 302, 303, 307 | |||
raise(Redirection.new(response)) | |||
when 200...400 | |||
unless @response_array_key.blank? | |||
begin | |||
new_response_body = ActiveSupport::JSON.decode(response.body)[@response_array_key] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like in the wrong place. Why do we need to handle JSON here? Should not the Format take care of it like it do actually?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rafaelfranca I moved it to the JSON Format class, please check it.
This is interesting. I actually just started to investigate this when trying to get a jsonapi compatible api working with active resource. Does this work on singleton resources in addition to collections? It doesn't look like it from the commit. Maybe it should also include that functionality? In that case, may want to consider renaming the configurable option to response_key? Also, not sure if response_key is the most descriptive, but I don't have any better suggestions yet. :) |
@rafaelfranca can you please check and let me know If you have any other comments? :) |
This issue has been automatically marked as stale because it has not been commented on for at least three months. If it is an issue and you can still reproduce this error on the If it is a pull request and you are still interested on having it merged please make sure it can be merged clearly. Thank you for all your contributions. |
Thank you for the pull request and sorry for the delay. Active Resource is a very opinionated client and I feel like we can't support all formats of json response that exists. By adding this option we may give the wrong impression that it should be possible to configure Active Resource to consume any json API but this is not the case. That said I prefer to not support this. |
Some
APIs
doesn't follow the known standards and they're returning the json array or the json object inside a key for example :And now ActiveResource doesn't understand these APIs so I added an optional attribute called
response_array_key
you can set its value inside theActiveResource
model to solve this issue.So now if you added
self.response_array_key = 'data'
to yourActiveResource
model it will understand your API and if you left it empty nothing will happen.