mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Add include raw data option in request body.
This commit is contained in:
		
				
					committed by
					
						 Debadutta Panda
						Debadutta Panda
					
				
			
			
				
	
			
			
			
						parent
						
							92a5a28072
						
					
				
				
					commit
					f5fa05d184
				
			| @@ -25,11 +25,22 @@ | ||||
|         <option value="patch">PATCH</option> | ||||
|         </select> | ||||
|     </div> | ||||
|     <div class="form-row form-row-http-in-upload hide"> | ||||
|  | ||||
|     <div id="form-reqBody-http-in-controller" class="form-row hide" style="display: flex;"> | ||||
|         <label> </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 style="display: flex; margin-left: 5px; flex-direction: column-reverse; gap:35%;"> | ||||
|             <div id="form-row-http-in-upload" class="hide" style="display: flex;"> | ||||
|                 <input type="checkbox" id="node-input-upload" style="margin-right: 5%; margin-bottom: 5%;"> | ||||
|                 <label for="node-input-upload" style="text-wrap: nowrap;" data-i18n="httpin.label.upload"></label> | ||||
|             </div> | ||||
|             <div id="form-row-http-in-rawdata" class="hide" style="display: flex;"> | ||||
|                 <input type="checkbox" id="node-input-includeRawBody" style="margin-right: 5%; margin-bottom: 5%;"> | ||||
|                 <label for="node-input-includeRawBody" style="text-wrap: nowrap;" data-i18n="httpin.label.rawBody"></label> | ||||
|             </div> | ||||
|         </div> | ||||
|     </div> | ||||
|      | ||||
|  | ||||
|     <div class="form-row"> | ||||
|         <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"> | ||||
| @@ -74,6 +85,7 @@ | ||||
|                   label:RED._("node-red:httpin.label.url")}, | ||||
|             method: {value:"get",required:true}, | ||||
|             upload: {value:false}, | ||||
|             includeRawBody: {value:false}, | ||||
|             swaggerDoc: {type:"swagger-doc", required:false} | ||||
|         }, | ||||
|         inputs:0, | ||||
| @@ -115,16 +127,22 @@ | ||||
|                 $('.row-swagger-doc').hide(); | ||||
|             } | ||||
|             $("#node-input-method").on("change", function() { | ||||
|                 if ($(this).val() === "post") { | ||||
|                     $(".form-row-http-in-upload").show(); | ||||
|                 var method = $(this).val(); | ||||
|                 if(["post", "put", "patch","delete"].includes(method)){ | ||||
|                     $("#form-reqBody-http-in-controller").show(); | ||||
|                     $("#form-row-http-in-rawdata").show(); | ||||
|                     if (method === "post") { | ||||
|                         $("#form-row-http-in-upload").show(); | ||||
|                     } else { | ||||
|                         $("#form-row-http-in-upload").hide(); | ||||
|                     } | ||||
|                 } else { | ||||
|                     $(".form-row-http-in-upload").hide(); | ||||
|                     $("#form-row-http-in-rawdata").hide(); | ||||
|                     $("#form-row-http-in-upload").hide(); | ||||
|                     $("#form-reqBody-http-in-controller").hide(); | ||||
|                 } | ||||
|             }).change(); | ||||
|  | ||||
|  | ||||
|         } | ||||
|  | ||||
|     }); | ||||
|     var headerTypes = [ | ||||
|         {value:"content-type",label:"Content-Type",hasValue: false}, | ||||
|   | ||||
| @@ -26,16 +26,12 @@ module.exports = function(RED) { | ||||
|     var mediaTyper = require('media-typer'); | ||||
|     var isUtf8 = require('is-utf8'); | ||||
|     var hashSum = require("hash-sum"); | ||||
|  | ||||
|     function rawBodyParser(req, res, next) { | ||||
|         if (req.skipRawBodyParser) { next(); } // don't parse this if told to skip | ||||
|         if (req._body) { return next(); } | ||||
|         req.body = ""; | ||||
|         req._body = true; | ||||
|  | ||||
|         var isText = true; | ||||
|         var checkUTF = false; | ||||
|  | ||||
|      | ||||
|     function rawBodyParser(req, _res, next) { | ||||
|         if(!req._nodeRedReqStream) { | ||||
|             return next(); | ||||
|         } | ||||
|         var isText = true, checkUTF = false; | ||||
|         if (req.headers['content-type']) { | ||||
|             var contentType = typer.parse(req.headers['content-type']) | ||||
|             if (contentType.type) { | ||||
| @@ -51,28 +47,29 @@ module.exports = function(RED) { | ||||
|                     && (parsedType.subtype !== "x-protobuf")) { | ||||
|                     checkUTF = true; | ||||
|                 } else { | ||||
|                     // application/octet-stream or application/cbor | ||||
|                     isText = false; | ||||
|                 } | ||||
|  | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         getBody(req, { | ||||
|         getBody(req._nodeRedReqStream, { | ||||
|             length: req.headers['content-length'], | ||||
|             encoding: isText ? "utf8" : null | ||||
|         }, function (err, buf) { | ||||
|             if (err) { return next(err); } | ||||
|             if (err) {  | ||||
|                 return next(err);  | ||||
|             } | ||||
|             if (!isText && checkUTF && isUtf8(buf)) { | ||||
|                 buf = buf.toString() | ||||
|             } | ||||
|             req.body = buf; | ||||
|             next(); | ||||
|             Object.defineProperty(req, "rawRequestBody", { | ||||
|                 value: buf, | ||||
|                 enumerable: true | ||||
|             }) | ||||
|             return next(); | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     var corsSetup = false; | ||||
|  | ||||
|     function createRequestWrapper(node,req) { | ||||
|         // This misses a bunch of properties (eg headers). Before we use this function | ||||
|         // need to ensure it captures everything documented by Express and HTTP modules. | ||||
| @@ -188,6 +185,7 @@ module.exports = function(RED) { | ||||
|             } | ||||
|             this.method = n.method; | ||||
|             this.upload = n.upload; | ||||
|             this.includeRawBody = n.includeRawBody; | ||||
|             this.swaggerDoc = n.swaggerDoc; | ||||
|  | ||||
|             var node = this; | ||||
| @@ -227,7 +225,9 @@ module.exports = function(RED) { | ||||
|             } | ||||
|  | ||||
|             var maxApiRequestSize = RED.settings.apiMaxLength || '5mb'; | ||||
|  | ||||
|             var jsonParser = bodyParser.json({limit:maxApiRequestSize}); | ||||
|  | ||||
|             var urlencParser = bodyParser.urlencoded({limit:maxApiRequestSize,extended:true}); | ||||
|  | ||||
|             var metricsHandler = function(req,res,next) { next(); } | ||||
| @@ -254,25 +254,35 @@ module.exports = function(RED) { | ||||
|                 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); | ||||
|                 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,multipartParser,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); | ||||
|                 RED.httpNode.put(this.url, cookieParser(), httpMiddleware, corsHandler, metricsHandler, jsonParser, urlencParser, rawBodyParser, this.callback, this.errorHandler); | ||||
|             } else if (this.method == "patch") { | ||||
|                 RED.httpNode.patch(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); | ||||
|                 RED.httpNode.patch(this.url, cookieParser(), httpMiddleware, corsHandler, metricsHandler, jsonParser, urlencParser, rawBodyParser, this.callback, this.errorHandler); | ||||
|             } else if (this.method == "delete") { | ||||
|                 RED.httpNode.delete(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); | ||||
|                 RED.httpNode.delete(this.url, cookieParser(), httpMiddleware, corsHandler, metricsHandler, jsonParser, urlencParser, rawBodyParser, this.callback, this.errorHandler); | ||||
|             } | ||||
|              | ||||
|             // unique id for httpInNodeSettings based on method name and url path | ||||
|             var httpInNodeId = `${this.method.toLowerCase()}:${this.url}` | ||||
|  | ||||
|             // get httpInNodeSettings from RED.httpNode | ||||
|             var httpInNodes = RED.httpNode.get('httpInNodes') | ||||
|  | ||||
|             // Add httpInNode to RED.httpNode | ||||
|             httpInNodes.set(httpInNodeId, this); | ||||
|              | ||||
|             this.on("close",function() { | ||||
|                 // remove httpInNodeSettings from RED.httpNode | ||||
|                 httpInNodes.remove(httpInNodeId) | ||||
|                 var node = this; | ||||
|                 RED.httpNode._router.stack.forEach(function(route,i,routes) { | ||||
|                     if (route.route && route.route.path === node.url && route.route.methods[node.method]) { | ||||
| @@ -284,9 +294,9 @@ module.exports = function(RED) { | ||||
|             this.warn(RED._("httpin.errors.not-created")); | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     RED.nodes.registerType("http in",HTTPIn); | ||||
|  | ||||
|  | ||||
|     function HTTPOut(n) { | ||||
|         RED.nodes.createNode(this,n); | ||||
|         var node = this; | ||||
| @@ -361,5 +371,6 @@ module.exports = function(RED) { | ||||
|             done(); | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     RED.nodes.registerType("http response",HTTPOut); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user