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
1 changed files with 22 additions and 2 deletions

View File

@ -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;