POST JSON data to simple rails application with curl

11,075

Solution 1

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.

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

Share:
11,075
delta2006
Author by

delta2006

Updated on July 20, 2022

Comments

  • delta2006
    delta2006 almost 2 years

    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.

  • delta2006
    delta2006 over 12 years
    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.
  • Jonathan Julian
    Jonathan Julian over 12 years
    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
  • delta2006
    delta2006 over 12 years
    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
    delta2006 over 12 years
    as you can see, it started posting, but in the end, the data added is nil.
  • delta2006
    delta2006 over 12 years
    I should clarify I am in a windows 32 environment.
  • delta2006
    delta2006 over 12 years
    problem solved. thanks so much. Jonathan did get us started on the right track. thank.
  • sorens
    sorens about 8 years
    Time to explore. Try: meyerweb.com/eric/tools/dencoder to get you started.