question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware send text/html instead of application/json

See original GitHub issue

What 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
< 
{&quot;status&quot;:400,&quot;statusText&quot;:&quot;Bad Request&quot;,&quot;errors&quot;:[{&quot;field&quot;:&quot;page&quot;,&quot;location&quot;:&quot;body&quot;,&quot;messages&quot;:[&quot;\&quot;page\&quot; must be larger than or equal to 1&quot;],&quot;types&quot;:[&quot;number.min&quot;]}]}
* Closing connection 0

How to change content-type and prevent html escaping?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
IrynaBucommented, Sep 19, 2016

This saved me 😃 router.use((err, req,res,next)=>{ if (err instanceof ValidationError){ res.status(err.status).json(err); } else { res.send(err); } });

2reactions
ClintEsteMaderacommented, May 14, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found