curl命令行POST

我正在尝试模拟我的简单Rails脚手架Web服务的POST。 脚手架创建的文件尚未更改。 当我从网站表单发布数据时,会正确创建记录。

当我尝试使用curl进行POST时,会在数据库中创建一条记录,但它在字段中具有NULL值,但“updated_at”和“created_at”除外。 我是命令行curl的新手,所以我可能没有正确地做到这一点。

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

我从WEBrick那里得到了这个:

开始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’)重定向到http:// localhost:3000 / flintstones / 4

为json做了GET之后

curl localhost:3000 / flintstones.json

我收到:

[{ “打火石:{” 名称 “:NULL,” created_at “:” 2011-04-29T00:09:58Z”, “的updated_at”: “2011-04-29T00:09:58Z”, “ID”:4, “朋友”:空}}]

为什么我的字段中为空?

提前致谢。

我用google搜索了一下,每个使用curl和rails web服务的例子都显示了格式传递的参数

object[paramName]=paramValue

以及每个参数的一个-d集,这将使您的CURL语句看起来像

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

以下是我引用的来源:

  • 如何使用cURL测试RESTful Rails
  • 使用cURL测试RESTful Rails Web服务

Rails(默认情况下)不会以提供的方式查找post正文,“ name=Gazoo&friend=Rubble ”。 它寻找这个方案 – 鉴于你的模型是Flintstones,你的字段是名字和朋友 – “ Flintstone[name]=Gazoo&Flintstone[friend]=Rubble ”。 这是用于post身体的铁路DSL来自表格post。