From 1aff82242a5a917a3fb57c3228f0de1b330c42dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Nu=C3=B1ez?= Date: Mon, 29 Jan 2018 21:28:50 -0300 Subject: [PATCH 1/3] Add support for JSON POST Add support for JSON POST --- lib/document_handler.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/document_handler.js b/lib/document_handler.js index 68985ec..376aeb1 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -52,6 +52,7 @@ DocumentHandler.prototype.handlePost = function (request, response) { var _this = this; var buffer = ''; var cancelled = false; + var ct = request.headers['content-type']; // What to do when done var onSuccess = function () { @@ -65,6 +66,19 @@ DocumentHandler.prototype.handlePost = function (request, response) { ); return; } + + //added support for json data + if (ct && ct.split(';')[0] === 'application/json') { + try { + let json = JSON.parse(buffer); + buffer = json.data; + } catch(e) { + winston.verbose('error adding document'); + response.writeHead(500, { 'content-type': 'application/json' }); + response.end(JSON.stringify({ message: 'invalid JSON received.' })); + } + } + // And then save if we should _this.chooseKey(function (key) { _this.store.set(key, buffer, function (res) { @@ -83,7 +97,6 @@ DocumentHandler.prototype.handlePost = function (request, response) { }; // If we should, parse a form to grab the data - var ct = request.headers['content-type']; if (ct && ct.split(';')[0] === 'multipart/form-data') { var busboy = new Busboy({ headers: request.headers }); busboy.on('field', function (fieldname, val) { From 038abb7c3bd4d8e8e065c7a2d9a602a3e6b29803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Nu=C3=B1ez?= Date: Mon, 29 Jan 2018 22:04:45 -0300 Subject: [PATCH 2/3] forgot return on invalid json --- lib/document_handler.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/document_handler.js b/lib/document_handler.js index 376aeb1..ff4d9bb 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -76,6 +76,7 @@ DocumentHandler.prototype.handlePost = function (request, response) { winston.verbose('error adding document'); response.writeHead(500, { 'content-type': 'application/json' }); response.end(JSON.stringify({ message: 'invalid JSON received.' })); + return; } } From ec3aac6aa981e15708d7616c1b6f5c302c08e8d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Nu=C3=B1ez?= Date: Mon, 29 Jan 2018 22:07:08 -0300 Subject: [PATCH 3/3] changed default content type to text/plain --- static/application.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/application.js b/static/application.js index d936cd9..4390061 100644 --- a/static/application.js +++ b/static/application.js @@ -64,7 +64,7 @@ haste_document.prototype.save = function(data, callback) { type: 'post', data: data, dataType: 'json', - contentType: 'application/json; charset=utf-8', + contentType: 'text/plain; charset=utf-8', success: function(res) { _this.locked = true; _this.key = res.key;