Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Rest controller format / mime type
  • 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?
  • The REST controller returns data in the format requested. You can find how it determines the format in the docs: http://fuelphp.com/docs/general/controllers/rest.html#/format_determination

    Since a REST controller returns a data structure (internally, an array), text is a very illogical return format. What would be the use-case for that?
  • 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.
  • Harro, is it possible to specify attributes in the generated XML? I can't see how to do that...
  • HarroHarro
    Accepted Answer
    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:

    $this->response->body($mycustomxmlvalue);
    $this->response->set_header('Content-Type', 'application/xml']);
    return $this->response;
  • 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.

    Thanks, Harro.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion