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:
parent
400071879f
commit
c97786e12c
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user