1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02: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:
Nick O'Leary 2019-06-08 20:42:14 +01:00
parent 400071879f
commit c97786e12c
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -203,8 +203,28 @@ module.exports = function(RED) {
var payload = null; var payload = null;
if (method !== 'GET' && method !== 'HEAD' && typeof msg.payload !== "undefined") { if (method !== 'GET' && method !== 'HEAD' && typeof msg.payload !== "undefined") {
if (opts.headers['content-type'] == 'multipart/form-data' && typeof payload === "object") { if (opts.headers['content-type'] == 'multipart/form-data' && typeof msg.payload === "object") {
opts.formData = msg.payload; 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 { } else {
if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) { if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) {
payload = msg.payload; payload = msg.payload;