Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #693 from gmethvin/master

Make bodyParser ignore the Content-Type if no body is present
  • Loading branch information...
commit 59b668db9cceefb333f016e62b34be3881a028de 2 parents 8262bd3 + 387bf89
@tj tj authored
View
4 lib/middleware/json.js
@@ -50,6 +50,8 @@ exports = module.exports = function(options){
if (req._body) return next();
req.body = req.body || {};
+ if (!utils.hasBody(req)) return next();
+
// check Content-Type
if ('application/json' != utils.mime(req)) return next();
@@ -76,4 +78,4 @@ exports = module.exports = function(options){
});
});
}
-};
+};
View
2  lib/middleware/multipart.js
@@ -61,6 +61,8 @@ exports = module.exports = function(options){
req.body = req.body || {};
req.files = req.files || {};
+ if (!utils.hasBody(req)) return next();
+
// ignore GET
if ('GET' == req.method || 'HEAD' == req.method) return next();
View
4 lib/middleware/urlencoded.js
@@ -48,6 +48,8 @@ exports = module.exports = function(options){
if (req._body) return next();
req.body = req.body || {};
+ if (!utils.hasBody(req)) return next();
+
// check Content-Type
if ('application/x-www-form-urlencoded' != utils.mime(req)) return next();
@@ -73,4 +75,4 @@ exports = module.exports = function(options){
});
});
}
-};
+};
View
12 lib/utils.js
@@ -16,6 +16,18 @@ var http = require('http')
, signature = require('cookie-signature');
/**
+ * Return `true` if the request has a body, otherwise return `false`.
+ *
+ * @param {IncomingMessage} req
+ * @return {Boolean}
+ * @api private
+ */
+
+exports.hasBody = function(req) {
+ return 'transfer-encoding' in req.headers || 'content-length' in req.headers;
+};
+
+/**
* Extract the mime type from the given request's
* _Content-Type_ header.
*
Please sign in to comment.
Something went wrong with that request. Please try again.