symfony/symfony — Issue #3215: Violations of route requirements do not lead to an error response with expected content-type
Author: tommyd3mdi
Created: 2012-01-29 22:58:15+00:00
State: open

(This is a follow-up for http://stackoverflow.com/questions/9035548)

I'm currently writing a REST service within a Symfony2 application. This is (roughly) how one of my controllers look like:

/**
* @Route("/account", defaults={"_format" = "json"})
*/
class AccountController extends APIController
{
/**
* @Route("/")
* @Method("get")
*/
public function listAction()
{
// process something here and return JSON-encoded response
}
}

This works all fine - i.e. if some problem pops up and I throw a `HTTPException`, the correct `error.json.twig` from Symfony2's TwigBundle is used and renders the HTTP error as JSON.

However, this is not true for the violation of route requirements, of which `@Method("get")` is one. If I for example throw a `POST` or `PUT` against the above URL, I still get the correct HTTP status code back, but rendered with the wrong content-type (text/html instead of application/json).

I'm very much interested to let the complete API return JSON responses and nothing else - the question is: How do I achieve that? Do I have to omit route requirements alltogether and check the requirements myself (and then eventually throw my own `HTTPException`s)? Is returning text/html the expected behaviour or is this a bug of some sort?