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.

2
  • it seems that the url parameters aren't getting passed to the sql insert and NULLs are getting inserted instead. Commented Apr 29, 2011 at 0:51
  • That's correct Dan. And my question is why? Commented Apr 29, 2011 at 1:14

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Goes to show how good formatting can make a blog post much more productive to readers.
In case you need authentification as well, look here: robots.thoughtbot.com/curling-with-rails-authenticity-token. Several parameters can also be combined using &
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.