Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl command line POST

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.

like image 566
Kurt Avatar asked Apr 29 '11 00:04

Kurt


People also ask

What is curl post command?

Curl is a command-line utility that allows users to create network requests. Curl is accessible on Windows, Linux, and Mac, making it the go-to choice for developers across all platforms. We can make POST requests with varying levels of detail.

Can you post using curl?

To POST a file with curl , simply add the @ symbol before the file location. The file can be an archive, image, document, etc.


2 Answers

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:

like image 153
Jeff Swensen Avatar answered Oct 01 '22 16:10

Jeff Swensen


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.

like image 42
eggie5 Avatar answered Oct 01 '22 16:10

eggie5