Skip to content
Browse files

Specific error message for content-type of JSON but empty body.

related to #680
  • Loading branch information...
1 parent 59b668d commit fbe9e0c480267d5041e47286b2d193ada7a795e6 @evdb evdb committed
Showing with 12 additions and 1 deletion.
  1. +7 −0 lib/middleware/json.js
  2. +5 −1 test/json.js
View
7 lib/middleware/json.js
@@ -66,6 +66,13 @@ exports = module.exports = function(options){
req.on('data', function(chunk){ buf += chunk });
req.on('end', function(){
var first = buf.trim()[0];
+
+ if ( buf.length == 0 ) {
+ // content-type says json, but no body. Provide specific error message to help
+ // debug. This is often due to a library not creating valid requests.
+ return next( utils.error( 400, "invalid json - request content-type is 'application/json' but request body empty") );
+ }
+
if (strict && '{' != first && '[' != first) return next(utils.error(400, 'invalid json'));
try {
req.body = JSON.parse(buf, options.reviver);
View
6 test/json.js
@@ -67,7 +67,11 @@ describe('connect.json()', function(){
app.request()
.post('/')
.set('Content-Type', 'application/json')
- .expect(400, done);
+ .end(function(res) {
+ res.should.have.status(400);
+ res.body.should.include("invalid json - request content-type is 'application/json' but request body empty");
+ done();
+ })
})
it('should support all http methods', function(done){

0 comments on commit fbe9e0c

Please sign in to comment.
Something went wrong with that request. Please try again.