15

I'm trying to simulate a POST to my simple Rails scaffold web service. The files created by the scaffold have not been changed. When I POST data from the website form, a record is created correctly.

When I attempt to POST with curl, a record is created in the database, but it has NULL values in the fields, except for 'updated_at' and 'created_at'. I'm new to command line curl so I may not be doing it correctly.

curl -d "name=Gazoo&friend=Rubble" localhost:3000/flintstones

I get back this from WEBrick:

Started POST "/flintstones" for 127.0.0.1 at Thu Apr 28 18:04:47 -0600 2011 Processing by FlintstonesController#create as Parameters: {"name"=>"Gazoo", "friend"=>"Rubble"} AREL (0.3ms) INSERT INTO "flintstones" ("name", "friend", "created_at", "updated_at") VALUES (NULL, NULL, '2011-04-29 00:04:47.779902', '2011-04-29 00:04:47.779902') Redirected to http://localhost:3000/flintstones/4

After a GET for the json

curl localhost:3000/flintstones.json

I receive:

[{"flintstone:{"name":null,"created_at":"2011-04-29T00:09:58Z","updated_at":"2011-04-29T00:09:58Z","id":4,"friend":null}}]

Why do I get null in my fields?

Thanks in advance.

Kurt
  • 855
  • 2
  • 12
  • 22

2 Answers2

23

I've googled a bit and every example of using curl with a rails web service shows the parameters passed in the format

object[paramName]=paramValue

as well as one -d set for each parameter which would make your CURL statement look like

curl -d "flintstone[name]=Gazoo" -d "flintstone[friend]=Rubble" localhost:3000/flintstones

Here are the sources I'm referencing:

Jeff Swensen
  • 3,513
  • 28
  • 52
  • Thank you Jeff. I had seen the second source you cited, which wasn't helpful. The first one, which I hadn't seen, is helpful. Your solution above did the trick. Now on to using ASIHttpFormRequest or RestKIT for my trial iOS app. – Kurt Apr 29 '11 at 01:13
  • Goes to show how good formatting can make a blog post much more productive to readers. – Jeff Swensen Apr 29 '11 at 01:39
  • In case you need authentification as well, look here: https://robots.thoughtbot.com/curling-with-rails-authenticity-token. Several parameters can also be combined using & – Zane Nov 08 '16 at 18:39
3

Rails (by default) doesn't look for a post body in the way provided, "name=Gazoo&friend=Rubble". It looks for this scheme - given your model is Flintstones and your fields are name and friend - "Flintstone[name]=Gazoo&Flintstone[friend]=Rubble". This is rail's DSL for post body from a form post.

eggie5
  • 1,920
  • 18
  • 25