diff --git a/lib/plugins/bodyParser.js b/lib/plugins/bodyParser.js index 1d77ca978..e42331fed 100644 --- a/lib/plugins/bodyParser.js +++ b/lib/plugins/bodyParser.js @@ -162,6 +162,12 @@ function bodyParser(options) { var parser; var type = req.contentType().toLowerCase(); + var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); + // map any +json to application/json + if (jsonPatternMatcher.test(type)) { + type = 'application/json'; + } + switch (type) { case 'application/json': parser = parseJson[0]; diff --git a/lib/plugins/jsonBodyParser.js b/lib/plugins/jsonBodyParser.js index 174b5eba4..97b20eaed 100644 --- a/lib/plugins/jsonBodyParser.js +++ b/lib/plugins/jsonBodyParser.js @@ -28,8 +28,13 @@ function jsonBodyParser(options) { // save original body on req.rawBody and req._body req.rawBody = req._body = req.body; - if (req.getContentType() !== 'application/json' || !req.body) { - return next(); + var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); + var contentType = req.getContentType(); + + if (contentType !== 'application/json' || !req.body) { + if (!jsonPatternMatcher.test(contentType)) { + return next(); + } } var params; diff --git a/package.json b/package.json index 02430330b..bc914b33d 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "Falco Nogatz", "Gergely Nemeth", "Guillaume Chauvet", + "Ifiok Idiang", "Isaac Schlueter", "Jacob Quatier", "James O'Cull", diff --git a/test/plugins/jsonBodyParser.test.js b/test/plugins/jsonBodyParser.test.js index c0ce2b7ca..431b477f3 100644 --- a/test/plugins/jsonBodyParser.test.js +++ b/test/plugins/jsonBodyParser.test.js @@ -489,4 +489,33 @@ describe('JSON body parser', function() { client.write('{"malformedJsonWithPercentSign":30%}'); client.end(); }); + + it('should handle application/*+json as application/json', function(done) { + SERVER.use(restify.plugins.bodyParser({ maxBodySize: 1024 })); + + SERVER.post('/', function(req, res, next) { + res.send(200, { length: req.body.length }); + next(); + }); + + var opts = { + hostname: '127.0.0.1', + port: PORT, + path: '/', + method: 'POST', + agent: false, + headers: { + accept: 'application/json', + 'content-type': 'application/hal+json', + 'transfer-encoding': 'chunked' + } + }; + var client = http.request(opts, function(res) { + assert.equal(res.statusCode, 413); + res.once('end', done); + res.resume(); + }); + client.write('{"a":[' + new Array(512).join('1,') + '0]}'); + client.end(); + }); });