From 05bc31fa9e53c8b404b60fce4ec6bf06ea133a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Fri, 18 Aug 2023 11:51:24 +0200 Subject: [PATCH] fix(Webhook Node): Backward compatible form-data parsing for non-array fields --- packages/cli/src/WebhookHelpers.ts | 5 +++++ packages/cli/test/integration/webhooks.api.test.ts | 6 ++++-- packages/nodes-base/nodes/Webhook/Webhook.node.ts | 6 +++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/WebhookHelpers.ts b/packages/cli/src/WebhookHelpers.ts index 0ef649d0ab16e..91e8c21a0544b 100644 --- a/packages/cli/src/WebhookHelpers.ts +++ b/packages/cli/src/WebhookHelpers.ts @@ -309,6 +309,11 @@ export async function executeWebhook( }); req.body = await new Promise((resolve) => { form.parse(req, async (err, data, files) => { + for (const key in data) { + if (Array.isArray(data[key]) && data[key].length === 1) { + data[key] = data[key][0]; + } + } resolve({ data, files }); }); }); diff --git a/packages/cli/test/integration/webhooks.api.test.ts b/packages/cli/test/integration/webhooks.api.test.ts index f03740dd0287f..b47613a6964c1 100644 --- a/packages/cli/test/integration/webhooks.api.test.ts +++ b/packages/cli/test/integration/webhooks.api.test.ts @@ -113,7 +113,9 @@ describe('Webhook API', () => { test('should handle multipart/form-data', async () => { const response = await agent .post('/webhook/abcd') - .field('field', 'value') + .field('field1', 'value1') + .field('field2', 'value2') + .field('field2', 'value3') .attach('file', Buffer.from('random-text')) .set('content-type', 'multipart/form-data'); @@ -125,7 +127,7 @@ describe('Webhook API', () => { file: [file], }, } = response.body.body; - expect(data).toEqual({ field: ['value'] }); + expect(data).toEqual({ field1: 'value1', field2: ['value2', 'value3'] }); expect(file.mimetype).toEqual('application/octet-stream'); expect(readFileSync(file.filepath, 'utf-8')).toEqual('random-text'); }); diff --git a/packages/nodes-base/nodes/Webhook/Webhook.node.ts b/packages/nodes-base/nodes/Webhook/Webhook.node.ts index fb870c71695db..281cf06287844 100644 --- a/packages/nodes-base/nodes/Webhook/Webhook.node.ts +++ b/packages/nodes-base/nodes/Webhook/Webhook.node.ts @@ -127,7 +127,7 @@ export class Webhook extends Node { const response: INodeExecutionData = { json: { headers: req.headers, - params: req.params, + params: {}, query: req.query, body: req.body, }, @@ -207,7 +207,7 @@ export class Webhook extends Node { binary: {}, json: { headers: req.headers, - params: req.params, + params: {}, query: req.query, body: data, }, @@ -263,7 +263,7 @@ export class Webhook extends Node { binary: {}, json: { headers: req.headers, - params: req.params, + params: {}, query: req.query, body: {}, },