Skip to content

Commit cca4365

Browse files
committed
support multipart
1 parent 33e772f commit cca4365

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

packages/insomnia/src/main/importers/importers/curl.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,34 @@ describe('curl', () => {
8787
},
8888
],
8989
},
90+
{
91+
name: 'should handle multipart',
92+
curl: 'curl https://insomnia.rest/signup -F file=@/home/user/file.txt -F foo=bar -F baz=qux',
93+
expected: [
94+
{
95+
body: {
96+
mimeType: 'multipart/form-data',
97+
params: [
98+
{
99+
fileName: '/home/user/file.txt',
100+
name: 'file',
101+
type: 'file',
102+
},
103+
{
104+
name: 'foo',
105+
type: 'text',
106+
value: 'bar',
107+
},
108+
{
109+
name: 'baz',
110+
type: 'text',
111+
value: 'qux',
112+
},
113+
],
114+
},
115+
},
116+
],
117+
},
90118
{
91119
name: 'should handle --data with url-encoded ampersand',
92120
curl: "curl -X POST https://example.com -H 'Content-Type: application/x-www-form-urlencoded' --data 'first=1&second=2'",

packages/insomnia/src/main/importers/importers/curl.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,19 @@ export const convert: Converter = rawData => {
3333
// This preserves existing behaviour, if there are bugs they will be here.
3434
const transformCurlDataObjectToInsomniaBody = (
3535
data: curlconverter.JSONOutput['data'],
36+
files: curlconverter.JSONOutput['files'],
3637
mimeType: string,
3738
): ImportRequest['body'] => {
3839
if (!data) {
39-
return { mimeType };
40+
return {};
41+
}
42+
// Handle multipart form data with files and text fields
43+
if (files) {
44+
const f = files ? Object.entries(files).map(([name, fileName]) => ({ name, fileName, type: 'file' })) : [];
45+
return {
46+
params: [...f, ...(Object.entries(data).map(([name, value]) => ({ name, value, type: 'text' })) as Parameter[])],
47+
mimeType: 'multipart/form-data',
48+
};
4049
}
4150
// Handle non-form-urlencoded bodies as text
4251
if (mimeType !== 'application/x-www-form-urlencoded') {
@@ -86,7 +95,7 @@ const transformCurlDataObjectToInsomniaBody = (
8695
};
8796
const transformCurlObjectToInsomniaRequest = (output: curlconverter.JSONOutput): ImportRequest => {
8897
const mimeType = output.headers?.['Content-Type'] || '';
89-
const body = transformCurlDataObjectToInsomniaBody(output.data, mimeType);
98+
const body = transformCurlDataObjectToInsomniaBody(output.data, output.files, mimeType);
9099

91100
// Match our auth types to curlconverter
92101
const mapAuthTypeToInsomniaAuthType: Record<AuthType, SupportedAuthTypes> = {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
curl http://192.168.1.1:9200/executions/_search\?pretty --data '{"query":{"match_all":{}}}'
1+
curl http://192.168.1.1:9200/executions/_search\?pretty --json '{"query":{"match_all":{}}}'

0 commit comments

Comments
 (0)