I’m writing an API in Cake2.1.3 using the REST instructions in the docs. Having previously written one in Cake1.3, I would use the now-deprecated cakeError class to issue a response with an HTTP code in the format it was requested in. For example, if I requested http://example.com/widgets/view/invalid-code.json, cakeError would dispatch a 400 error code (and message) in the JSON format. Cake 2.x uses exceptions to handle this. According to the docs:
throw new BadRequestException()
issues a 400 HTTP code with a “Bad Request” message, but it only seems to do so in the HTML format. Can it automatically respond in the format it was requested in?
From my knowledge there’s no way to do this automatically, but you could write your own exception handler that checks the request and outputs the appropriate content type.