Rails “array” in xml to params

2019-06-05 07:43发布

I am designing a rails rest API that I would like to expose either as xml or json. In a specific action, I am submitting a collection of objects like this:

curl -d "<posts><post><title>my title</title></post><post><title>second post</title></post></posts>" localhost:3000/posts -X POST

I thought that this would be translated into

> params[:posts]
[{:title => "my title"}, {:title => "second post"}]

Instead of that I get

 {:post => [{:title => "my title"}, {:title => "second post"}]}

The question is then how should I format my xml such that I get this in params[:posts] ?

[{:title => "my title"}, {:title => "second post"}]

I know how to do it in functional tests and json but not xml :(

1条回答
唯我独甜
2楼-- · 2019-06-05 08:28

Looks like the XML in your curl starts with:

<posts></post>...

rather than:

<posts><post>...

Edit:

Posting XML as an array in Rails requires type="array" to be added to the array node.

<posts type="array"><post>...
查看更多
登录 后发表回答