POST a JSON array to a Controller

I would like to write a Controller to which a client can pass a JSON Array (e.g. using POST).

Example:

POST /index.php/apps/myapp/api
[{id:5,data:"xyz"},{id:8,data:"xyz"}]

Unfortunately, I found no way, how to access this array in the Controller, since there is no variable Name, I can use as Parameter in my Controller method.

Is this possible, or would I have to embed my Array in a JSON object?
e.g.: {items: [{id:5,data:"xyz"},{id:8,data:"xyz"}]}

Then, I could use the variable $items as parameter in my Controller. However, the JSON string would be more complicated. Is there a better way?

You can embed it in a JSON object like the one above and then just use $items as variable name in your method. Another way would be to simply use $this->request->params for the whole JSON object AFAIK.

1 Like