Curl POST哈希数组

时间:2015-01-13 17:38:46

标签: ruby-on-rails bash curl

我正在尝试重现cURLPOST哈希数组并且语法有问题。这是我想要的基本格式:

curl -X POST http://localhost:3000/api/v1/shops -d \
  "shop[users][]=1 \
   &shop[users][]=2\
   &shop[users][]=c"

如何将值12c变为哈希?

在我的Rails API上,我希望对象看起来像这样:

{
  shop: {
    users: [
      {
        name: "Foo",
        age: 34
      },
      {
        name: "Bar",
        age: 12
      }
    ]
  }
}

更新

我尝试了下面的here-doc解决方案,但Rails解析了错误,给了我:

{"{  shop: {    users: "=>{"      {        name: \"Foo\",        age: 34      },      {        name: \"Bar\",        age: 12      }    "=>{"  }}"=>nil}},"action"=>"create", "controller"=>"shops"}

2 个答案:

答案 0 :(得分:0)

尝试使用 here-doc

执行此操作
$ curl -X POST http://localhost:3000/api/v1/shops -d "@-" <<EOF
{
  shop: {
    users: [
      {
        name: "Foo",
        age: 34
      },
      {
        name: "Bar",
        age: 12
      }
    ]
  }
}
EOF

答案 1 :(得分:0)

我遇到了同样的问题并且不想使用json文件,所以我发现这种方式也可以正常工作! (至少使用Rails 3)

curl -i http://localhost:3000/team/999/transfers \
 -X "POST" \
 -d "team[transfers][][player_in_id]=1" \
 -d "team[transfers][][player_out_id]=2" \
 -d "team[transfers][][player_in_id]=3" \
 -d "team[transfers][][player_out_id]=4"