I find myself constantly fighting with the format (mime type) returned by the Rest controller. Might it be possible to add a $format parameter to Rest::response()?
Also, could 'text' ('text/plain') be one of the supported formats?
Thanks, Chris
Oh - and can't an empty body (with no content) be a valid REST response?
In the API I'm developing, a GET request on a resource will normally return XML on success, but a 'not found' will return a 404 status with text/plain content-type and an empty body.
Similarly, a successful POST will return a 201 status with a text/plain content-type and an id in the body, whereas POST which fails validation will return a 403 with an error message in a text/plain body.
I guess I could return XML for everything, but it felt like overkill for a simple id or error message...
(Also, I didn't see a straightforward way of setting attributes by generating XML from arrays, so I use SimpleXML to build the XML).
I think you have missed the way the REST controller works.
In a good API design, you allow the client to determine the format it wants returned, and it does so using the HTTP-ACCEPT header. If you set that to 'text/xml' on the request, the REST controller will return XML. If you set it to "application/json", you'll get JSON returned. No additional code needed. You can override the use of the HTTP-HEADER in your code/config, as explained on that docs page.
Internally it works with an array, the conversion to whatever response format is neeeded happens automatically.
This also means you have to be consistent. If the client wants XML back, it should get XML back, doesn't matter what it is you send back, and what the returned HTTP status is.
If you have the REST controller deal with it, your action will return an array which does not have the functionality to add attributes, array's are just key->value.
If you want to set a specific return value in your action, use:
Ok, that makes sense - for straightforward requirements, the REST controller will bake it for you, for more specialised requirements, you need to put it together yourself. That's fine.