My API returns HTML on error instead of JSON response.
If I rise throw new BadRequestException('aaa'); HTML with debug and profiler is returned instead of JSON.
I use fos/rest 3.0.5
my fos_rest.yaml:
fos_rest:
format_listener:
enabled: true
rules:
- { path: '^/api', priorities: [ json, xml ], fallback_format: json, prefer_extension: true }
- { path: '^/api/doc$', priorities: [ 'text/html', '*/*' ], fallback_format: json, prefer_extension: false }
- { path: '^/sign', priorities: [ 'text/html', '*/*' ], fallback_format: json, prefer_extension: false }
versioning:
enabled: true
resolvers:
media_type:
enabled: true
body_converter:
enabled: true
validate: true
validation_errors_argument: validationErrors
view:
view_response_listener: 'force'
formats:
json: true
xml: true
exception:
enabled: false
codes:
'Symfony\Component\HttpKernel\Exception\NotFoundHttpException': 404
'Symfony\Component\HttpKernel\Exception\BadRequestHttpException': 400
'Symfony\Component\Security\Core\Exception\AccessDeniedException': 403
'Symfony\Component\Security\Core\Exception\DisabledException': 403
'App\Exceptions\AppException': 500
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
messages:
'App\Exceptions\AppException': true
allowed_methods_listener: true
body_listener:
enabled: true
default_format: json
decoders:
json: fos_rest.decoder.json
param_fetcher_listener:
enabled: true
force: true
the response I get (removed rubbish):
POST https://127.0.0.1:8000/api/request/017a57a0-d223-b171-beae-372f3ea6e901/786867/cs
HTTP/1.1 400 Bad Request
Allow: POST
Cache-Control: no-cache, private
Content-Type: text/html; charset=UTF-8
Date: Wed, 30 Jun 2021 09:59:08 GMT
X-Debug-Exception: aaa
X-Debug-Exception-File: %2Fhome%2Fjan%2Fpacketa%2Fapp%2Fzsign%2Fvendor%2Fsymfony%2Fhttp-kernel%2FHttpKernel.php:82
X-Robots-Tag: noindex
Transfer-Encoding: chunked
<!-- aaa (400 Bad Request) -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="robots" content="noindex,nofollow"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<title>aaa (400 Bad Request)</title>
<style>/* This file is based on WebProfilerBundle/Resources/views/Profiler/profiler.css.twig.
If you make any change in this file, verify the same change is needed in the other file. */
</style>
</head>
<body>
<script>
document.body.classList.add(
localStorage.getItem('symfony/profiler/theme') || (matchMedia('(prefers-color-scheme: dark)').matches ? 'theme-dark' : 'theme-light')
);
</script>
<header>
<div class="container">
</body>
</html>
<!-- aaa (400 Bad Request) -->
Response code: 400 (Bad Request); Time: 746ms; Content length: 95726 bytes
I would prefer to set up ErrorController but it seems not to work for 3.0 anymore. Any idea? or example? Thanks a lot for help.
After a long time searching for what seems a similar situation as yours, I was able to find a solution which would fix 400 errors return as json rather than html.
Simply add in:
This was while using JMSSerializerBundle and worked for me, hopefully it will be of use for others.