使用curl模拟具有嵌套资源数组的表单帖子

Pab*_* B. 4 xml rest curl ruby-on-rails http

如何使用curl在使用application/x-www-form-urlencoded而不是application/xml的嵌套属性的表单上创建http帖子?

使用XML它可以正常工作:

curl -i -X 'POST' -H 'Content-Type: application/xml' http://localhost:3000 -d '<user><name>John Doe</name><emails><email><address>jdoe@gmail.com</address></email><email><address>jdoe@yahoo.com</address></email></emails></user>'
Run Code Online (Sandbox Code Playgroud)

结果如下:

Parameters: {"action"=>"profile", "controller"=>"users", "user"=>{"name"=>"John Doe", "emails"=>{"email"=>[{"address"=>"jdoe@gmail.com"}, {"address"=>"jdoe@yahoo.com"}]}}}
Run Code Online (Sandbox Code Playgroud)

但是,我试图在没有xml的情况下完成相同的结果.

我试过这样的:

curl -i -X POST -d 'user[name]=John Doe&user[emails][email][address]=jdoe@gmail.com&user[emails][email][address]=jdoe@yahoo.com' http://localhost:3000/
Run Code Online (Sandbox Code Playgroud)

但它没有奏效:

Parameters: {"user"=>{"name"=>"John Doe", "emails"=>{"email"=>{"address"=>"jdoe@yahoo.com"}}}}
Run Code Online (Sandbox Code Playgroud)

小智 9

请你试试以下内容:

curl -i -X POST -d 'user[name]=John Doe&user[emails][][email][address]=jdoe@gmail.com&user[emails][][email][address]=jdoe@yahoo.com' http://localhost:3000/
Run Code Online (Sandbox Code Playgroud)

注意[]背后[emails]