Middleware send text/html instead of application/json
See original GitHub issueWhat I get from validation is this:
< HTTP/1.1 100 Continue
< HTTP/1.1 400 Bad Request
< X-Powered-By: Express
< X-Content-Type-Options: nosniff
< Content-Type: text/html; charset=utf-8
< Content-Length: 299
< Date: Wed, 27 Jul 2016 10:19:37 GMT
< Connection: keep-alive
* HTTP error before end of send, stop sending
<
{"status":400,"statusText":"Bad Request","errors":[{"field":"page","location":"body","messages":["\"page\" must be larger than or equal to 1"],"types":["number.min"]}]}
* Closing connection 0
How to change content-type and prevent html escaping?
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Express router sending HTML headers instead of JSON using ...
I have an API router.post that is sending the wrong headers, I need the headers to be set to application/json; charset=utf-8 however they ......
Read more >API Reference - Express 4.x
Returns middleware that only parses JSON and only looks at requests where the Content-Type header matches the type option.
Read more >[L5] Symfony(?) forcing text/html Content-Type when response ...
For consistency, I would like to return the same Content-Type in my API, so I created a middleware, which gets the job done....
Read more >Laravel: Convert all responses to JSON automatically
When that occurs, your app will nonchalantly send a HTML response, which is not cool. The Middleware. Well, there is very simple way...
Read more >Force JSON response on all API routes in Laravel
Second, register the created middleware in app/Http/Kernel.php. ... For example 3 on the image above will be still text/html response.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This saved me 😃 router.use((err, req,res,next)=>{ if (err instanceof ValidationError){ res.status(err.status).json(err); } else { res.send(err); } });
It is worth mentioning that this error handling middleware has to be defined after all the routes have been defined, otherwise, it won’t work.