Skip to content
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
92 changes: 92 additions & 0 deletions packages/insomnia/src/main/importers/importers/curl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,98 @@ describe('curl', () => {
expected: { body: { text: 'key=value' } },
},

// --form / -F flags (multipart/form-data)
{
name: 'should handle --form with plain key=value',
curl: "curl -X POST https://example.com -H 'Content-Type: multipart/form-data' --form 'field=value'",
expected: {
body: {
mimeType: 'multipart/form-data',
params: [{ name: 'field', value: 'value', type: 'text' }],
},
},
},
{
name: 'should handle --form with @filename',
curl: "curl -X POST https://example.com -H 'Content-Type: multipart/form-data' --form 'file=@/tmp/a.json'",
expected: {
body: {
mimeType: 'multipart/form-data',
params: [{ name: 'file', fileName: '/tmp/a.json', type: 'file' }],
},
},
},
{
name: 'should strip ;type= modifier from --form file value',
curl: "curl -X POST https://example.com -H 'Content-Type: multipart/form-data' --form 'data=@/tmp/a.json;type=application/json'",
expected: {
body: {
mimeType: 'multipart/form-data',
params: [{ name: 'data', fileName: '/tmp/a.json', type: 'file' }],
},
},
},
{
name: 'should strip ;filename= modifier from --form file value',
curl: "curl -X POST https://example.com -H 'Content-Type: multipart/form-data' --form 'upload=@/tmp/a.bin;filename=renamed.bin'",
expected: {
body: {
mimeType: 'multipart/form-data',
params: [{ name: 'upload', fileName: '/tmp/a.bin', type: 'file' }],
},
},
},
{
name: 'should strip multiple chained modifiers from --form file value',
curl: "curl -X POST https://example.com -H 'Content-Type: multipart/form-data' --form 'upload=@/tmp/a.bin;type=application/octet-stream;filename=renamed.bin'",
expected: {
body: {
mimeType: 'multipart/form-data',
params: [{ name: 'upload', fileName: '/tmp/a.bin', type: 'file' }],
},
},
},
{
name: 'should strip ;type= modifier from --form text value',
curl: "curl -X POST https://example.com -H 'Content-Type: multipart/form-data' --form 'field=value;type=text/csv'",
expected: {
body: {
mimeType: 'multipart/form-data',
params: [{ name: 'field', value: 'value', type: 'text' }],
},
},
},
{
name: 'should preserve semicolons in quoted --form text value',
curl: `curl -X POST https://example.com -H 'Content-Type: multipart/form-data' --form 'colors="red; green; blue";type=text/x-myapp'`,
expected: {
body: {
mimeType: 'multipart/form-data',
params: [{ name: 'colors', value: 'red; green; blue', type: 'text' }],
},
},
},
{
name: 'should preserve semicolons in quoted --form filename',
curl: `curl -X POST https://example.com -H 'Content-Type: multipart/form-data' --form 'file=@"local;file";filename="name;in;post"'`,
expected: {
body: {
mimeType: 'multipart/form-data',
params: [{ name: 'file', fileName: 'local;file', type: 'file' }],
},
},
},
{
name: 'should handle -F alias the same as --form',
curl: "curl -X POST https://example.com -H 'Content-Type: multipart/form-data' -F 'data=@/tmp/a.json;type=application/json'",
expected: {
body: {
mimeType: 'multipart/form-data',
params: [{ name: 'data', fileName: '/tmp/a.json', type: 'file' }],
},
},
},

// -H flags
{
name: 'should handle -H with space after colon',
Expand Down
42 changes: 38 additions & 4 deletions packages/insomnia/src/main/importers/importers/curl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ const extractCookieHeaderValue = (pairsByName: PairsByName) => {
})
.join('; ');
};
const getUnquotedSemicolonIndex = (value: string) => {
let isQuoted = false;
for (let i = 0; i < value.length; i++) {
const char = value[i];
if (char === '\\' && isQuoted) {
i++;
continue;
}
if (char === '"') {
isQuoted = !isQuoted;
continue;
}
if (char === ';' && !isQuoted) {
return i;
}
}
return -1;
};
const unquoteFormValue = (value: string) => {
if (value.startsWith('"') && value.endsWith('"')) {
return value.slice(1, -1).replace(/\\(["\\])/g, '$1');
}
return value;
};
const extractBody = (
dataParameters: Parameter[],
pairsByName: PairsByName,
Expand All @@ -168,16 +192,26 @@ const extractBody = (
...((pairsByName.form as string[] | undefined) || []),
...((pairsByName.F as string[] | undefined) || []),
].map(str => {
const [name, value] = str.split('=');
const equalsIndex = str.indexOf('=');
if (equalsIndex === -1) {
return { name: str, value: '', type: 'text' } as Parameter;
}
const name = str.slice(0, equalsIndex);
const rawValue = str.slice(equalsIndex + 1);
// curl --form values support trailing modifiers separated by unquoted `;`,
// e.g. `@file;type=application/json`, while quoted form content may also
// contain semicolons. See `curl --manual`.
const semiIndex = getUnquotedSemicolonIndex(rawValue);
const content = semiIndex === -1 ? rawValue : rawValue.slice(0, semiIndex);
const item: Parameter = {
name,
};

if (value.indexOf('@') === 0) {
item.fileName = value.slice(1);
if (content.indexOf('@') === 0) {
item.fileName = unquoteFormValue(content.slice(1));
item.type = 'file';
} else {
item.value = value;
item.value = unquoteFormValue(content);
item.type = 'text';
}

Expand Down