diff --git a/lib/request.js b/lib/request.js index d1a73ff..33c4b34 100644 --- a/lib/request.js +++ b/lib/request.js @@ -319,7 +319,13 @@ module.exports = { var type = this.get('Content-Type'); if (!type) return ''; - return contentType.parse(type).parameters.charset || ''; + try { + type = contentType.parse(type); + } catch (e) { + return ''; + } + + return type.parameters.charset || ''; }, /** diff --git a/test/request/charset.js b/test/request/charset.js index 12eb09b..2d7a582 100644 --- a/test/request/charset.js +++ b/test/request/charset.js @@ -26,5 +26,11 @@ describe('req.charset', function(){ req.header['content-type'] = 'text/plain; charset=utf-8'; req.charset.should.equal('utf-8'); }) + + it('should return "" if content-type is invalid', function(){ + var req = request(); + req.header['content-type'] = 'application/json; application/text; charset=utf-8'; + req.charset.should.equal(''); + }) }) })