diff --git a/lib/request.js b/lib/request.js index 21a5a9d..a1be181 100644 --- a/lib/request.js +++ b/lib/request.js @@ -315,10 +315,16 @@ module.exports = { */ get charset() { - const type = this.get('Content-Type'); + let 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 ee6f260..2d44abb 100644 --- a/test/request/charset.js +++ b/test/request/charset.js @@ -26,5 +26,11 @@ describe('req.charset', () => { req.header['content-type'] = 'text/plain; charset=utf-8'; req.charset.should.equal('utf-8'); }); + + it('should return "" if content-type is invalid', () => { + const req = request(); + req.header['content-type'] = 'application/json; application/text; charset=utf-8'; + req.charset.should.equal(''); + }); }); });