ruby-on-rails - curl 命令行 POST

标签 ruby-on-rails curl

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

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

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

我从 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



在 GET JSON 之后

curl localhost:3000/flintstones.json



我收到:

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

为什么我的字段为空?

提前致谢。

最佳答案

我在谷歌上搜索了一下,每个将 curl 与 Rails Web 服务一起使用的示例都显示了以格式传递的参数
object[paramName]=paramValue
还有一个-d为每个参数设置这将使您的 CURL 语句看起来像
curl -d "flintstone[name]=Gazoo" -d "flintstone[friend]=Rubble" localhost:3000/flintstones
以下是我引用的来源:

  • How to Use cURL to Test RESTful Rails
  • Using cURL to test RESTful Rails Web Services
  • 关于ruby-on-rails - curl 命令行 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5826690/

    相关文章:

    php - 无法在 PHP 7.3.9 中启用 Curl

    c - 在没有安全协议(protocol)的网页上使用curl

    curl - 为什么来自同一台计算机的不同客户端的两个请求缓存未命中云端?

    ruby-on-rails - 如何在大 CSV 出错后重试 Postgres COPY

    ruby-on-rails - 强制 Spree Commerce 在生产模式下禁用 SSL

    css - 如何在一个 SCSS 中获取 mixin,以便在 Rails 元素的所有 SCSS 文件中得到识别?

    curl - 带有 CURL 的 HTTP2 给出 "Unsupported Protocol"

    css - 添加到这个 Rails form_for 中的这个类有什么问题吗?

    ruby-on-rails - Rails 是否必须延迟加载所有内容?或者是吗?

    PHPcurl自动身份验证不设置 session