mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Improve handling of file upload in request node
formData can only be Strings or Buffers - anything else will cause errors. To help matters, we now look for invalid types and json-encode them where needed.
This commit is contained in:
		| @@ -203,8 +203,28 @@ module.exports = function(RED) { | ||||
|             var payload = null; | ||||
|  | ||||
|             if (method !== 'GET' && method !== 'HEAD' && typeof msg.payload !== "undefined") { | ||||
|                 if (opts.headers['content-type'] == 'multipart/form-data' && typeof payload === "object") { | ||||
|                     opts.formData = msg.payload; | ||||
|                 if (opts.headers['content-type'] == 'multipart/form-data' && typeof msg.payload === "object") { | ||||
|                     opts.formData = {}; | ||||
|  | ||||
|                     for (var opt in msg.payload) { | ||||
|                         if (msg.payload.hasOwnProperty(opt)) { | ||||
|                             var val = msg.payload[opt]; | ||||
|                             if (val !== undefined && val !== null) { | ||||
|                                 if (typeof val === 'string' || Buffer.isBuffer(val)) { | ||||
|                                     opts.formData[opt] = val; | ||||
|                                 } else if (typeof val === 'object' && val.hasOwnProperty('value')) { | ||||
|                                     // Treat as file to upload - ensure it has an options object | ||||
|                                     // as request complains if it doesn't | ||||
|                                     if (!val.hasOwnProperty('options')) { | ||||
|                                         val.options = {}; | ||||
|                                     } | ||||
|                                     opts.formData[opt] = val; | ||||
|                                 } else { | ||||
|                                     opts.formData[opt] = JSON.stringify(val); | ||||
|                                 } | ||||
|                             } | ||||
|                         } | ||||
|                     } | ||||
|                 } else { | ||||
|                     if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) { | ||||
|                         payload = msg.payload; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user