Skip to content

fix(editor): Import multipart form data from curl command correctly #14898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,45 @@ describe('useImportCurlCommand', () => {
expect(parameters.sendBody).toBe(false);
expect(parameters.options.allowUnauthorizedCerts).toBe(true);
});

test('Should parse json body data', () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test name 'Should parse json body data' is incorrect as the test is actually parsing form-urlencoded data, not JSON

const curl =
'curl -X POST https://reqbin.com/echo \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=authorization_code" \
-d "code=abc" \
-d "redirect_uri=https://test.app.n8n.cloud/webhook-test/12345" \
-d "client_id=app-4343434" \
-d "client_secret=secret"';
const parameters = toHttpNodeParameters(curl);

expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(true);
expect(parameters.bodyParameters).toEqual({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing verification of the contentType property which should be 'form-urlencoded'

parameters: [
{
name: 'grant_type',
value: 'authorization_code',
},
{
name: 'code',
value: 'abc',
},
{
name: 'redirect_uri',
value: 'https://test.app.n8n.cloud/webhook-test/12345',
},
{
name: 'client_id',
value: 'app-4343434',
},
{
name: 'client_secret',
value: 'secret',
},
],
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ const extractQueries = (queries: JSONOutput['queries'] = {}): HttpNodeQueries =>
};

const jsonBodyToNodeParameters = (body: JSONOutput['data'] = {}): Parameter[] | [] => {
if (typeof body === 'string') {
const parameters = body.split('&');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing URL-decoding for form-urlencoded data. Form data usually contains encoded characters (like %20 for spaces or + for spaces) that need decoding.


return parameters.map((parameter) => {
const [key, value] = parameter.split('=');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting by first '=' character can break values containing '=' characters. Form values might include URL parameters or Base64 data containing '=' characters.

return toKeyValueArray([key, value]);
});
}
return Object.entries(body).map(toKeyValueArray);
};

Expand Down Expand Up @@ -220,7 +228,6 @@ export const flattenObject = <T extends Record<string, unknown>>(obj: T, prefix

export const toHttpNodeParameters = (curlCommand: string): HttpNodeParameters => {
const curlJson = curlToJson(curlCommand);

const headers = curlJson.headers ?? {};

lowerCaseContentTypeKey(headers);
Expand Down