【问题标题】:curl json post request via terminal to a rails appcurl json 通过终端向 Rails 应用程序发布请求
【发布时间】:2011-08-05 05:40:09
【问题描述】:

我正在尝试使用来自 os x 终端的 curl 命令在我的 rails 应用程序上创建一个用户。无论我如何格式化数据,应用程序都会返回我的验证未通过的响应。

curl http://localhost:3000/api/1/users.json -i -X POST -d {"user":{"first_name":"firstname","last_name":"lastname","email":"email@email.com","password":"app123","password_confirmation":"app123"}}"

我已经尝试了每一种变化。我试过使用 [] 括号,我试过 user={data..} 似乎没有任何效果。有什么想法吗?

【问题讨论】:

  • 为什么不直接使用$ rails cUser.create :first_name => 'John', :last_name => 'Smith', ...
  • @coryward:这会破坏 API 的全部目的。
  • 真的让我难过
  • 嗨,Bob 可能发现了问题,但如果它可以帮助,这是我昨天找到的帖子:squarism.com/2011/04/01/how-to-write-a-ruby-rails-3-rest-api 这是关于 XML,但它可能会有所帮助。最好的问候。

标签: json ruby-on-rails-3 macos curl terminal


【解决方案1】:

首先,命令末尾有一个多余的 "。

试试这个

curl -v \
  -H "Accept: application/json" \
  -H "Content-type: application/json" \
  -X POST \
  -d ' {"user":{"first_name":"firstname","last_name":"lastname","email":"email@email.com","password":"app123","password_confirmation":"app123"}}' \
  http://localhost:3000/api/1/users

【讨论】:

  • 就是这样。我昨晚发现它需要 -H Accept 和 Content-type。
  • 嗨,我可以调用这样的脚本:abc.com/?a=4&b={"x":"y"}。你能告诉我它在 curl 终端中会是什么样子吗??
  • 嗯,在我的情况下,我需要用引号将目标 URL 括起来
  • 我能够从上面删除 -v(详细)开关和 -H "Accept:..." ,它对我来说仍然可以正常工作。
  • 认为你也可以删除 -X POST,因为你使用 -d
猜你喜欢
  • 1970-01-01
  • 2020-01-18
  • 2014-07-28
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多