Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON exception in cakephp 3

Tags:

I'm doing a restfull api in cakephp... And sometime i have some throw exceptions. For example:

if (!$this->request->is('post')) {
            throw new MethodNotAllowedException("The requested resource does not support http method " . $this->request->param('_method'));
        }

My problem is when the url is /controller/action.json the response is :

{
message: "The requested resource does not support http method GET",
url: "/api/auth/users/authenticate.json",
code: 405
}

In json format, but, when my url is /controller/action. My response is HTML, i want to know if is possible to force these exceptions to be always json without putting .json in the url.

Thanks!

like image 799
TacticJuls Avatar asked May 04 '17 18:05

TacticJuls


1 Answers

You can force exceptions to be always rendered in json adding in Controller/ErrorController.php (in beforeRender)

$this->RequestHandler->renderAs($this, 'json');
like image 195
fiblan Avatar answered Sep 21 '22 10:09

fiblan