From 64daaeb3102b4f348f748c4b18bafe863d88a49a Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 16 Jan 2017 22:39:30 +0000 Subject: [PATCH] Add file upload support to HTTP In node --- nodes/core/io/21-httpin.html | 18 ++++++++++++++++++ nodes/core/io/21-httpin.js | 15 ++++++++++++++- nodes/core/locales/en-US/messages.json | 3 ++- package.json | 1 + 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/nodes/core/io/21-httpin.html b/nodes/core/io/21-httpin.html index 0eae67423..4802314bc 100644 --- a/nodes/core/io/21-httpin.html +++ b/nodes/core/io/21-httpin.html @@ -25,6 +25,11 @@ +
+ + + +
@@ -59,6 +64,9 @@ To send JSON encoded data to the node, the content-type header of the request must be set to application/json.

+

+ If file uploads are enabled for POST requests, the files will be available under + msg.req.files.

Note: This node does not send any response to the http request. This should be done with a subsequent HTTP Response node. @@ -119,6 +127,7 @@ msg.cookies = { name: {value:""}, url: {value:"",required:true}, method: {value:"get",required:true}, + upload: {value:false}, swaggerDoc: {type:"swagger-doc", required:false} }, inputs:0, @@ -159,6 +168,15 @@ msg.cookies = { if(!RED.nodes.getType("swagger-doc")){ $('.row-swagger-doc').hide(); } + $("#node-input-method").change(function() { + if ($(this).val() === "post") { + $(".form-row-http-in-upload").show(); + } else { + $(".form-row-http-in-upload").hide(); + } + }).change(); + + } }); diff --git a/nodes/core/io/21-httpin.js b/nodes/core/io/21-httpin.js index 6d6bf7fbf..418dc7444 100644 --- a/nodes/core/io/21-httpin.js +++ b/nodes/core/io/21-httpin.js @@ -17,6 +17,7 @@ module.exports = function(RED) { "use strict"; var bodyParser = require("body-parser"); + var multer = require("multer"); var cookieParser = require("cookie-parser"); var getBody = require('raw-body'); var cors = require('cors'); @@ -177,6 +178,7 @@ module.exports = function(RED) { } this.url = n.url; this.method = n.method; + this.upload = n.upload; this.swaggerDoc = n.swaggerDoc; var node = this; @@ -225,10 +227,21 @@ module.exports = function(RED) { }; } + var multipartParser = function(req,res,next) { next(); } + if (this.upload) { + var mp = multer({ storage: multer.memoryStorage() }).any(); + multipartParser = function(req,res,next) { + mp(req,res,function(err) { + req._body = true; + next(err); + }) + }; + } + if (this.method == "get") { RED.httpNode.get(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler); } else if (this.method == "post") { - RED.httpNode.post(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); + RED.httpNode.post(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,multipartParser,rawBodyParser,this.callback,this.errorHandler); } else if (this.method == "put") { RED.httpNode.put(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); } else if (this.method == "patch") { diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index d36139f58..f6cb9113a 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -313,7 +313,8 @@ "method": "Method", "url": "URL", "doc": "Docs", - "return": "Return" + "return": "Return", + "upload": "Accept file uploads?" }, "setby": "- set by msg.method -", "basicauth": "Use basic authentication", diff --git a/package.json b/package.json index 005b0e2ae..f54f315a9 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "jsonata":"1.0.10", "media-typer": "0.3.0", "mqtt": "2.2.1", + "multer": "1.2.1", "mustache": "2.3.0", "nopt": "3.0.6", "oauth2orize":"1.7.0",