POST JSON data to simple rails application with curl POST JSON data to simple rails application with curl curl curl

POST JSON data to simple rails application with curl


To go along with what Jonathan said, the Post is now sending the data to the EntriesController. Now in your create action you have to get the data from the params hash. I am going to assume you are doing it the railsy way and so you would do something like this:

    curl -d 'entry[content]=I belong to AAA' -d entry[title]=AAA http://localhost:3000/entries'

In your controller

    Entry.create(params[:entry])

This says grab the "entry" data from the params hash (created by rails for you) and pass it as a parameter to Entry to initialize a new Object. "create" will do a "new" and "save" for you in one method call.


I ran a test and got the error MultiJson::DecodeError (743: unexpected token at '{'content':'I belong to AAA','title':'AAA'}'):

JSON requires double quotes for keys and strings, not single quotes. Try --data '{"content":"I belong to AAA","title":"AAA"}'


take a block of JSON

{\"a\":\"this_is_a\"}

and url encode it

%7B%22a%22%3A%22this_is_a%22

and then use curl to post it

curl -i --data "working_params=%7B%22a%22%3A%22this_is_a%22" http://url/accepts/json