-
Notifications
You must be signed in to change notification settings - Fork 403
fix: Unable to parse response body the throw the HTTP error #1509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Could you try |
Thank you for the answer.
I have tried I am using option |
I don't really understand how it could return both html and json. Could you setup a simple reproduction repo for this so I can check it? |
It will take some some to boil down my app which is production-ready, but if you need to for investigation, I'll prepare it.
Simply if MVC application also has REST API. @Controller
class BlogPostController {
/* === MVC ======================== */
@View("views/BlogPost.hbs")
protected renderBlogPostPage(): Promise<BlogPost> {
// ... Transactions with DB
return {
// ...
}
}
/* === REST ======================== */
protected addBlogPost(): Promise<void> {
// ... Transactions with DB
// Once done frontend can redirect to blog post page thus does not need the response data
}
} |
Sorry for keep you waiting. Here is the reproduction: As you can see, there is still |
Ok I think I understand your problem now. Keep your view methods html, so For the json methods, inject @Get('/api/sample')
protected sampleHandlerJson(@Res() res: Response) {
res.status(404);
res.json();
return res;
}
@Get('/api/sample/working')
protected sampleHandlerJsonWithoutError(@Res() res: Response) {
res.json({myParam: 'myValue'});
return res;
} |
Alternatively, if you only want to use json for errors, you can do this response handling in the error handler. |
Description
According the documentation,
Unfortunately, when I tried to throw the
BadRequestError
, I can not access to response data:According to response headers, the
Content-Type
hastext/html; charset=utf-8
type:Same as browser developer tools can not parse it, the
text()
method of fetch API fails.Minimal code-snippet showcasing the problem
Expected behavior
The response with
content-type: application/json; charset=utf-8
header and following content will be submitted to client:Actual behavior
The response has
content-type: text/html; charset=utf-8
header and non-parseable content.The text was updated successfully, but these errors were encountered: