10

I set up a simple new rails application with model entry, with attributes title and content using scaffolding.

now I am trying to use curl to POST the JSON data (rather than using the browser).

the following seems to work (i.e. successfully posted with null data):

curl --verbose --header "Accept: application/json" --header "Content-type: application/json" --request POST --data "" http://localhost:3000/entries

the following does not work:

curl --verbose --header "Accept: application/json" --header "Content-type: application/json" --request POST --data "{'content':'I belong to AAA','title':'AAA'}" http://localhost:3000/entries

I have tried many variations. the errors I get are mostly host not found or unexpected token at the JSON data.

3 Answers 3

6

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.

1
  • problem solved. thanks so much. Jonathan did get us started on the right track. thank.
    – delta2006
    Nov 9, 2011 at 23:46
2

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"}'

5
  • thanks but still getting 743 error even with your suggestion. I am wondering if somehow the application is not expecting or liking this json data I am trying to POST. it is a simple rails app created by rails new and the model is by rails generate scaffold.
    – delta2006
    Nov 8, 2011 at 5:13
  • Actually, this solution assumed you wanted to pass JSON in the body of the request, which is different from POSTing data like a browser does. To mimic a browser, just use curl -d 'content=I belong to AAA' -d title=AAA http://localhost:3000/entries Nov 9, 2011 at 4:38
  • Started POST "/entries" for 127.0.0.1 at 2011-11-09 07:46:48 -0500 Processing by EntriesController#create as / Parameters: {"content"=>"I belong to AAA", "title"=>"AAA"} ←[1m←[35mSQL (0.0ms)←[0m INSERT INTO "entries" ("content", "created_at", "tit le", "updated_at") VALUES (?, ?, ?, ?) [["content", nil], ["created_at", Wed, 0 9 Nov 2011 12:46:48 UTC +00:00], ["title", nil], ["updated_at", Wed, 09 Nov 2011 12:46:48 UTC +00:00]] Redirected to localhost:3000/entries/46 Completed 302 Found in 0ms
    – delta2006
    Nov 9, 2011 at 12:47
  • as you can see, it started posting, but in the end, the data added is nil.
    – delta2006
    Nov 9, 2011 at 12:48
  • I should clarify I am in a windows 32 environment.
    – delta2006
    Nov 9, 2011 at 12:49
0

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
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.