10.
Request Format: Why Exceptions Return HTML
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
5 Comments
Hey Guys!
Any ideas how to set default json format at Symfony 5? :)
Thanks!
Hey Bartlomeij !
There are 2 ways to do this:
1) Assuming you're using annotation routes, in your config/packages/routes/annotations.yaml file, you can do what we did in this video:
2) The above approach will make all annotation routes use JSON by default. But, if a user goes to a 404 page that doesn't match any route, they would still have an HTML. To fix that, you would register a listener on the kernel.request event (also called the RequestEvent), get the request, and call
$request->setRequestFormat('json').Let me know if that helps!
Cheers!
Just thinking ahead, but I hope I can just override a method in ApiProblemException that returns a customized Response :)
Hey Johan!
Unfortunately, there's nothing built in for that, but I like your thinking :). Of course, we have the HttpException class (really, the HttpExceptionInterface) and its sub-classes, but the default ExceptionListener in Symfony (which handles the response during an exception... by default), only uses this interface to get the status code and any headers you might want set. It still *always* renders this internal controller that always returns HTML. Well, as we learned here, it can also return JSON, but it's this more-or-less hardcoded, generic JSON page - you don't have a lot of control.
So, good thinking... but it doesn't work that way :).
Cheers!
Maybe in Symfony 4, haha ;)
I love your elaborate answers, thanks!
"Houston: no signs of life"
Start the conversation!