24

我有一个请求 API 的 iPhone 应用程序,它使用嵌套的参数结构来传递用户名和密码以登录。

在我的 Rails 控制器中,我通常使用以下方法成功检索此信息:

username = param[:session][:username]

我想使用 cURL 从 shell 测试我的 API。但我不知道如何提供嵌套参数?下面的代码不起作用......而且我尝试了多种变体。但似乎无法弄清楚?

curl -d '{"session":{"username":"test","password":"password"}}' http://testurl/remote/v1/sessions

任何帮助,非常感谢!

4

2 回答 2

61

不同的框架可能会以不同的方式解释 http 查询参数,但对于 Rails,您应该能够摆脱以下情况:

curl -d 'session[username]=name&session[password]=pwd' http://testurl/remote/v1/sessions
于 2010-10-05T03:54:45.473 回答
0

我发现这行得通。给定一个 JSON blob

{ "opts": {
    "finder": { 
      "cname":"superlatives"
    }
  }
}



curl -H "Content-Type: application/json" \
  -X POST \
  --data-binary '{"opts":{"finder":{"cname":"superlatives"}}}' \
  http://YOURSERVER.com/api/grams/one
于 2018-11-06T09:50:11.483 回答