AndrewKeig/express-validation

Middleware send text/html instead of application/json

Closed this issue ยท 10 comments

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?

I have the same problem and I can't find solution in google

I think this is solved with an error-handling function #31
Issue 31 is also regarding how to document it better.... this requirement is already documented in README, but probably it's not clear enough

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

Is there a way to configure express-validation to use application/json as default?
Looks Hacky above solution

It' documented in the README, maybe it's not clear enough and I really need community help to make it clearer.
This behaviour has been changed in the past to leverage express.js general error handling

It is possible for express-validation to directly reply a response, but that would be way short-sighted, as writing error-handling in medium/big applications it's a key role and I think it should be centralized to express.js, not having split responsabilities between express.js and express-validation. (that also means to change 2 points in the code)

Even with that approach I didn't had Lucky.
image
image

I'm taking a look at express-validation, it seems there's no reference to change res to html or something like that. I'd say that this is not an issue with the library itself.

@eduardomoroni Not sure if you've solved this already, but it seems you missed the last argument in your errorHandler. Express error-handling functions have four arguments (err, req, res, next) instead of three, so simply adding next should solve your problem.

@zsoltime, Hey dude. You got an Eagle Eye! Thanks so much for your help.
Actually, the next param was not being. When I added it, worked.

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.

Closing, released 3.0