I want to know how can I post json array directly to rails server. Say,
POST '/api/notes_bulk', HTTP/1.1
Content-Type: application/json
[{“content”:”content1”, “title”:”title1”}, {“content”:”content2”, “title”:”title2”}, {“content”:”content3”, “title”:”title3”}, {“content”:”content4”, “title”:”title4”}, {“content”:”content5”, “title”:”title5”}]
I did some search about it and all of the examples had some kind of key mapped to the array. While in my case, the json is an array at its top level.
How can I get the json data in the controller code? I notice that rails wraps this data with a "_json" key, but when I accessing it, it saysUnpermitted parameters: _json, ...
.
You can use that '_json' key to access data. To remove the warning you have to permit all the object keys from JSON array. In your case:
params.permit(_json: [:content, :title])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With