How to post multiple rows into DB using ruby Grape. For example when testing with CURL this is working fine
curl -H "Content-Type: application/json" -X POST \
-d '{"name": "test", "age": "22"}' http://localhost:3000/students
But this is not working
curl -H "Content-Type: application/json" -X POST \
-d '[{"name": "test", "age": "22"}, {"name": "someone", "age": "32" }]' \
http://localhost:3000/students
This is my grape api code
post do
student = Student.new
student.name = params[:name]
student.age = params[:age]
student.save!
end
You use a bad syntax for JSON array object. Try this:
EDIT :
If your payload is
{"name": "test", "age": "22"}
your code works. But you have a Array (params.kind_of?(Array)
).You can make this:
Explanation: