From 3fb532d0e48119d48d0f531b3a0240bf441914f6 Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Wed, 10 Apr 2024 11:09:20 +0300 Subject: [PATCH] fix(HTTP Request Node): Duplicate key names support for form data (#9040) Co-authored-by: Marcus --- .../nodes/HttpRequest/GenericFunctions.ts | 24 ++++++++++++++++++- .../nodes/HttpRequest/HttpRequest.node.ts | 3 ++- .../HttpRequest/V3/HttpRequestV3.node.ts | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts b/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts index 397537a009de7..945277ece5be5 100644 --- a/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts +++ b/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts @@ -7,7 +7,13 @@ import type { import set from 'lodash/set'; -export type BodyParameter = { name: string; value: string }; +import FormData from 'form-data'; + +export type BodyParameter = { + name: string; + value: string; + parameterType?: 'formBinaryData' | 'formData'; +}; export type IAuthDataSanitizeKeys = { [key: string]: string[]; @@ -168,6 +174,22 @@ export const prepareRequestBody = async ( set(result, entry.name, entry.value); return result; }, Promise.resolve({})); + } else if (bodyType === 'multipart-form-data' && version >= 4.2) { + const formData = new FormData(); + + for (const parameter of parameters) { + if (parameter.parameterType === 'formBinaryData') { + const entry = await defaultReducer({}, parameter); + const key = Object.keys(entry)[0]; + const data = entry[key] as { value: Buffer; options: FormData.AppendOptions }; + formData.append(key, data.value, data.options); + continue; + } + + formData.append(parameter.name, parameter.value); + } + + return formData; } else { return await reduceAsync(parameters, defaultReducer); } diff --git a/packages/nodes-base/nodes/HttpRequest/HttpRequest.node.ts b/packages/nodes-base/nodes/HttpRequest/HttpRequest.node.ts index c0d77e28dc0d1..a8317a289ae8f 100644 --- a/packages/nodes-base/nodes/HttpRequest/HttpRequest.node.ts +++ b/packages/nodes-base/nodes/HttpRequest/HttpRequest.node.ts @@ -14,7 +14,7 @@ export class HttpRequest extends VersionedNodeType { group: ['output'], subtitle: '={{$parameter["requestMethod"] + ": " + $parameter["url"]}}', description: 'Makes an HTTP request and returns the response data', - defaultVersion: 4.1, + defaultVersion: 4.2, }; const nodeVersions: IVersionedNodeType['nodeVersions'] = { @@ -23,6 +23,7 @@ export class HttpRequest extends VersionedNodeType { 3: new HttpRequestV3(baseDescription), 4: new HttpRequestV3(baseDescription), 4.1: new HttpRequestV3(baseDescription), + 4.2: new HttpRequestV3(baseDescription), }; super(nodeVersions, baseDescription); diff --git a/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts b/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts index 0e5fd4e3011c2..aadee86b46f34 100644 --- a/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts +++ b/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts @@ -49,7 +49,7 @@ export class HttpRequestV3 implements INodeType { this.description = { ...baseDescription, subtitle: '={{$parameter["method"] + ": " + $parameter["url"]}}', - version: [3, 4, 4.1], + version: [3, 4, 4.1, 4.2], defaults: { name: 'HTTP Request', color: '#0004F5',