From 90887779eaaddc54b0ce7a3a1820005041191cf3 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Sat, 8 Jun 2019 20:42:14 +0100 Subject: [PATCH] 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. --- .../@node-red/nodes/core/io/21-httprequest.js | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/io/21-httprequest.js b/packages/node_modules/@node-red/nodes/core/io/21-httprequest.js index d72e16bad..ec5290835 100644 --- a/packages/node_modules/@node-red/nodes/core/io/21-httprequest.js +++ b/packages/node_modules/@node-red/nodes/core/io/21-httprequest.js @@ -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;