Skip to content

Commit

Permalink
fix(Webhook Node): Backward compatible form-data parsing for non-arra…
Browse files Browse the repository at this point in the history
…y fields (#6967)
  • Loading branch information
netroy committed Aug 18, 2023
1 parent e8937aa commit db1958c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/cli/src/WebhookHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/test/integration/webhooks.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');
});
Expand Down
6 changes: 3 additions & 3 deletions packages/nodes-base/nodes/Webhook/Webhook.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -207,7 +207,7 @@ export class Webhook extends Node {
binary: {},
json: {
headers: req.headers,
params: req.params,
params: {},
query: req.query,
body: data,
},
Expand Down Expand Up @@ -263,7 +263,7 @@ export class Webhook extends Node {
binary: {},
json: {
headers: req.headers,
params: req.params,
params: {},
query: req.query,
body: {},
},
Expand Down

0 comments on commit db1958c

Please sign in to comment.