diff --git a/public/red/ui/editor.js b/public/red/ui/editor.js index 746f780bb..d41bd2a9a 100644 --- a/public/red/ui/editor.js +++ b/public/red/ui/editor.js @@ -236,7 +236,12 @@ RED.editor = (function() { //TODO: move this to RED.library var flowName = $("#node-input-filename").val(); if (!/^\s*$/.test(flowName)) { - $.post('library/flows/'+flowName,$("#node-input-filename").attr('nodes'),function() { + $.ajax({ + url:'library/flows/'+flowName, + type: "POST", + data: $("#node-input-filename").attr('nodes'), + contentType: "application/json; charset=utf-8" + }).done(function() { RED.library.loadFlowLibrary(); RED.notify("Saved nodes","success"); }); diff --git a/red/api/index.js b/red/api/index.js index ce655bf99..f0958b3b0 100644 --- a/red/api/index.js +++ b/red/api/index.js @@ -31,10 +31,10 @@ var errorHandler = function(err,req,res,next) { function init(adminApp) { - library.init(adminApp); - adminApp.use(express.json()); + library.init(adminApp); + // Editor if (!settings.disableEditor) { adminApp.get("/",ui.ensureSlash); diff --git a/red/api/library.js b/red/api/library.js index d7f792169..ec0bdcaf6 100644 --- a/red/api/library.js +++ b/red/api/library.js @@ -47,19 +47,19 @@ function createLibrary(type) { var path = req.params[0]; var fullBody = ''; req.on('data', function(chunk) { - fullBody += chunk.toString(); + fullBody += chunk.toString(); }); req.on('end', function() { - storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() { - res.send(204); - }).otherwise(function(err) { - util.log("[red] Error saving library entry '"+path+"' : "+err); - if (err.message.indexOf('forbidden') === 0) { - res.send(403); - return; - } - res.send(500); - }); + storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() { + res.send(204); + }).otherwise(function(err) { + util.log("[red] Error saving library entry '"+path+"' : "+err); + if (err.message.indexOf('forbidden') === 0) { + res.send(403); + return; + } + res.send(500); + }); }); }); } @@ -91,22 +91,16 @@ module.exports = { }); }, post: function(req,res) { - //TODO: do content-type properly - var fullBody = ''; - req.on('data', function(chunk) { - fullBody += chunk.toString(); - }); - req.on('end', function() { - storage.saveFlow(req.params[0],fullBody).then(function() { - res.send(204); - }).otherwise(function(err) { - util.log("[red] Error loading flow '"+req.params[0]+"' : "+err); - if (err.message.indexOf('forbidden') === 0) { - res.send(403); - return; - } - res.send(500); - }); + var flow = JSON.stringify(req.body); + storage.saveFlow(req.params[0],flow).then(function() { + res.send(204); + }).otherwise(function(err) { + util.log("[red] Error loading flow '"+req.params[0]+"' : "+err); + if (err.message.indexOf('forbidden') === 0) { + res.send(403); + return; + } + res.send(500); }); } } diff --git a/test/red/api/library_spec.js b/test/red/api/library_spec.js index 796d6b1b9..2f321724c 100644 --- a/test/red/api/library_spec.js +++ b/test/red/api/library_spec.js @@ -103,7 +103,7 @@ describe("library api", function() { var flow = '[]'; request(app) .post('/library/flows/foo') - .set('Content-Type', 'text/plain') + .set('Content-Type', 'application/json') .send(flow) .expect(204).end(function (err, res) { if (err) { diff --git a/test/red/api/nodes_spec.js b/test/red/api/nodes_spec.js index 0e683348a..cdd8b645e 100644 --- a/test/red/api/nodes_spec.js +++ b/test/red/api/nodes_spec.js @@ -150,6 +150,23 @@ describe("nodes api", function() { }); }); + it('returns 400 if request is invalid', function(done) { + var settingsAvailable = sinon.stub(settings,'available', function() { + return true; + }); + request(app) + .post('/nodes') + .send({}) + .expect(400) + .end(function(err,res) { + settingsAvailable.restore(); + if (err) { + throw err; + } + done(); + }); + }); + describe('by module', function() { it('installs the module and returns node info', function(done) { var settingsAvailable = sinon.stub(settings,'available', function() {