1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add file upload support to HTTP In node

This commit is contained in:
Nick O'Leary 2017-01-16 22:39:30 +00:00
parent 0646b0060e
commit 64daaeb310
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 35 additions and 2 deletions

View File

@ -25,6 +25,11 @@
<option value="patch">PATCH</option> <option value="patch">PATCH</option>
</select> </select>
</div> </div>
<div class="form-row form-row-http-in-upload hide">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-upload" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-upload" style="width: 70%;" data-i18n="httpin.label.upload"></label>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-url"><i class="fa fa-globe"></i> <span data-i18n="httpin.label.url"></span></label> <label for="node-input-url"><i class="fa fa-globe"></i> <span data-i18n="httpin.label.url"></span></label>
<input id="node-input-url" type="text" placeholder="/url"> <input id="node-input-url" type="text" placeholder="/url">
@ -59,6 +64,9 @@
To send JSON encoded data to the node, the content-type header of the request must be set to To send JSON encoded data to the node, the content-type header of the request must be set to
<code>application/json</code>. <code>application/json</code>.
</p> </p>
<p>
If file uploads are enabled for POST requests, the files will be available under
<code>msg.req.files</code>.
<p> <p>
<b>Note: </b>This node does not send any response to the http request. <b>Note: </b>This node does not send any response to the http request.
This should be done with a subsequent HTTP Response node. This should be done with a subsequent HTTP Response node.
@ -119,6 +127,7 @@ msg.cookies = {
name: {value:""}, name: {value:""},
url: {value:"",required:true}, url: {value:"",required:true},
method: {value:"get",required:true}, method: {value:"get",required:true},
upload: {value:false},
swaggerDoc: {type:"swagger-doc", required:false} swaggerDoc: {type:"swagger-doc", required:false}
}, },
inputs:0, inputs:0,
@ -159,6 +168,15 @@ msg.cookies = {
if(!RED.nodes.getType("swagger-doc")){ if(!RED.nodes.getType("swagger-doc")){
$('.row-swagger-doc').hide(); $('.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();
} }
}); });

View File

@ -17,6 +17,7 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var bodyParser = require("body-parser"); var bodyParser = require("body-parser");
var multer = require("multer");
var cookieParser = require("cookie-parser"); var cookieParser = require("cookie-parser");
var getBody = require('raw-body'); var getBody = require('raw-body');
var cors = require('cors'); var cors = require('cors');
@ -177,6 +178,7 @@ module.exports = function(RED) {
} }
this.url = n.url; this.url = n.url;
this.method = n.method; this.method = n.method;
this.upload = n.upload;
this.swaggerDoc = n.swaggerDoc; this.swaggerDoc = n.swaggerDoc;
var node = this; 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") { if (this.method == "get") {
RED.httpNode.get(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler); RED.httpNode.get(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler);
} else if (this.method == "post") { } 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") { } else if (this.method == "put") {
RED.httpNode.put(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); RED.httpNode.put(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
} else if (this.method == "patch") { } else if (this.method == "patch") {

View File

@ -313,7 +313,8 @@
"method": "Method", "method": "Method",
"url": "URL", "url": "URL",
"doc": "Docs", "doc": "Docs",
"return": "Return" "return": "Return",
"upload": "Accept file uploads?"
}, },
"setby": "- set by msg.method -", "setby": "- set by msg.method -",
"basicauth": "Use basic authentication", "basicauth": "Use basic authentication",

View File

@ -45,6 +45,7 @@
"jsonata":"1.0.10", "jsonata":"1.0.10",
"media-typer": "0.3.0", "media-typer": "0.3.0",
"mqtt": "2.2.1", "mqtt": "2.2.1",
"multer": "1.2.1",
"mustache": "2.3.0", "mustache": "2.3.0",
"nopt": "3.0.6", "nopt": "3.0.6",
"oauth2orize":"1.7.0", "oauth2orize":"1.7.0",