From 49bb9ed0faa5a863ba5736f7f633df817f7c70be Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 28 Feb 2017 05:26:48 -0800 Subject: [PATCH] http: always parse json for compatability. fixes #146. --- lib/http/base.js | 13 ++++++++++++- lib/http/server.js | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/http/base.js b/lib/http/base.js index 752977e4..4e537759 100644 --- a/lib/http/base.js +++ b/lib/http/base.js @@ -154,6 +154,7 @@ HTTPBase.prototype.handleRequest = co(function* handleRequest(req, res) { HTTPBase.prototype.parseBody = co(function* parseBody(req) { var body = Object.create(null); + var type = req.contentType; var data; if (req.method === 'GET') @@ -164,7 +165,10 @@ HTTPBase.prototype.parseBody = co(function* parseBody(req) { if (!data) return body; - switch (req.contentType) { + if (this.options.contentType) + type = this.options.contentType; + + switch (type) { case 'json': body = JSON.parse(data); break; @@ -507,6 +511,8 @@ function HTTPBaseOptions(options) { this.keyLimit = 100; this.bodyLimit = 20 << 20; + this.contentType = null; + if (options) this.fromOptions(options); } @@ -569,6 +575,11 @@ HTTPBaseOptions.prototype.fromOptions = function fromOptions(options) { this.ssl = options.ssl; } + if (options.contentType != null) { + assert(typeof options.contentType === 'string'); + this.contentType = options.contentType; + } + return this; }; diff --git a/lib/http/server.js b/lib/http/server.js index aa0baeca..62dcc77c 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -1734,6 +1734,7 @@ function HTTPOptions(options) { this.key = null; this.cert = null; this.ca = null; + this.contentType = 'json'; this.fromOptions(options); }