Skip to content

Commit 400002d

Browse files
committed
add support for multi part
1 parent 593ae2e commit 400002d

File tree

2 files changed

+62
-33
lines changed

2 files changed

+62
-33
lines changed

packages/insomnia/src/main/importers/importers/__snapshots__/index.test.ts.snap

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ exports[`Fixtures > Import curl > complex-input.sh 1`] = `
1717
},
1818
"body": {
1919
"mimeType": "application/json",
20-
"text": "{"email_id": "tem_123"}",
20+
"text": "{"email_id":"tem_123"}",
2121
},
2222
"headers": [
2323
{
24-
"name": "x-custom-header",
24+
"name": "x-custom-header ",
2525
"value": "foo bar",
2626
},
2727
{
@@ -30,12 +30,16 @@ exports[`Fixtures > Import curl > complex-input.sh 1`] = `
3030
},
3131
{
3232
"name": "Cookie",
33-
"value": "foo=bar; NID=91=iOf1sU9Ovlns9Dzn2Ipz05syr2K4AlZ4Kgp84eRVLf3_6DgcNrkqpWg4lfUvCB5cNxD26t",
33+
"value": "foo=bar",
3434
},
3535
{
3636
"name": "another-header",
3737
"value": "foo",
3838
},
39+
{
40+
"name": "Accept",
41+
"value": "application/json",
42+
},
3943
],
4044
"method": "POST",
4145
"name": "http://localhost:8000/api/v1/send",
@@ -59,7 +63,7 @@ exports[`Fixtures > Import curl > dollar-sign-input.sh 1`] = `
5963
"_type": "request",
6064
"authentication": {},
6165
"body": {
62-
"mimeType": "",
66+
"mimeType": "application/json",
6367
"text": "{"key":"TEST","websiteId":2,"storeId":4,"remove":true,"coupon":{"code":"erwrwer"}}",
6468
},
6569
"headers": [
@@ -77,7 +81,7 @@ exports[`Fixtures > Import curl > dollar-sign-input.sh 1`] = `
7781
},
7882
{
7983
"name": "Cookie",
80-
"value": "CookieTestConsent={stamp:\\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=='%2Cnecessary...",
84+
"value": "CookieTestConsent={stamp:'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=='%2Cnecessary...",
8185
},
8286
{
8387
"name": "Connection",
@@ -87,6 +91,14 @@ exports[`Fixtures > Import curl > dollar-sign-input.sh 1`] = `
8791
"name": "Pragma",
8892
"value": "no-cache",
8993
},
94+
{
95+
"name": "Content-Type",
96+
"value": "application/json",
97+
},
98+
{
99+
"name": "Accept",
100+
"value": "application/json",
101+
},
90102
],
91103
"method": "POST",
92104
"name": "https://test.dk",
@@ -229,6 +241,7 @@ exports[`Fixtures > Import curl > get-input.sh 1`] = `
229241
"name": "http://somesite.com/getdata",
230242
"parameters": [
231243
{
244+
"disabled": false,
232245
"name": "id",
233246
"value": "1234",
234247
},
@@ -372,29 +385,6 @@ exports[`Fixtures > Import curl > multi-input.sh 1`] = `
372385
}
373386
`;
374387

375-
exports[`Fixtures > Import curl > no-url-input.sh 1`] = `
376-
{
377-
"__export_date": "",
378-
"__export_format": 4,
379-
"__export_source": "insomnia.importers:v0.1.0",
380-
"_type": "export",
381-
"resources": [
382-
{
383-
"_id": "__REQ_1__",
384-
"_type": "request",
385-
"authentication": {},
386-
"body": {},
387-
"headers": [],
388-
"method": "POST",
389-
"name": "cURL Import 1",
390-
"parameters": [],
391-
"parentId": "__WORKSPACE_ID__",
392-
"url": "",
393-
},
394-
],
395-
}
396-
`;
397-
398388
exports[`Fixtures > Import curl > question-mark-input.sh 1`] = `
399389
{
400390
"__export_date": "",
@@ -407,10 +397,19 @@ exports[`Fixtures > Import curl > question-mark-input.sh 1`] = `
407397
"_type": "request",
408398
"authentication": {},
409399
"body": {
410-
"mimeType": "",
400+
"mimeType": "application/json",
411401
"text": "{"query":{"match_all":{}}}",
412402
},
413-
"headers": [],
403+
"headers": [
404+
{
405+
"name": "Content-Type",
406+
"value": "application/json",
407+
},
408+
{
409+
"name": "Accept",
410+
"value": "application/json",
411+
},
412+
],
414413
"method": "POST",
415414
"name": "http://192.168.1.1:9200/executions/_search",
416415
"parameters": [

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,25 @@ export const convertWithCurlConverter: Converter = (rawData: string) => {
529529
}
530530
}
531531
return parsed.map((cmd: any, index: number) => {
532-
const url = (cmd.url || '').replace(/\/$/, '');
532+
const rawUrl = (cmd.url || '').replace(/\/$/, '');
533+
534+
// Extract query parameters from URL and queries field
535+
let url = rawUrl;
536+
const parameters: Parameter[] = [];
537+
try {
538+
const parsed = new URL(rawUrl);
539+
if (parsed.search) {
540+
for (const [name, value] of parsed.searchParams.entries()) {
541+
parameters.push({ name, value, disabled: false });
542+
}
543+
url = rawUrl.replace(parsed.search, '');
544+
}
545+
} catch {}
546+
if (cmd.queries) {
547+
for (const [name, value] of Object.entries(cmd.queries)) {
548+
parameters.push({ name, value: String(value), disabled: false });
549+
}
550+
}
533551

534552
// Headers: convert from {name: value} object to [{name, value}] array
535553
const rawHeaders = Object.entries(cmd.headers || {}).map(([name, value]) => ({
@@ -566,7 +584,19 @@ export const convertWithCurlConverter: Converter = (rawData: string) => {
566584
const isUrlEncoded = mimeType === 'application/x-www-form-urlencoded';
567585

568586
let body: Record<string, any> = {};
569-
if (cmd.data !== undefined) {
587+
if (cmd.files) {
588+
// Multipart form data (from -F/--form flags)
589+
const params: any[] = [];
590+
for (const [name, fileName] of Object.entries(cmd.files)) {
591+
params.push({ name, fileName, type: 'file' });
592+
}
593+
if (cmd.data && typeof cmd.data === 'object') {
594+
for (const [name, value] of Object.entries(cmd.data)) {
595+
params.push({ name, value: String(value), type: 'text' });
596+
}
597+
}
598+
body = { mimeType: mimeType || 'multipart/form-data', params };
599+
} else if (cmd.data !== undefined) {
570600
if (isUrlEncoded) {
571601
const dataStr =
572602
typeof cmd.data === 'object'
@@ -602,7 +632,7 @@ export const convertWithCurlConverter: Converter = (rawData: string) => {
602632
_type: 'request',
603633
parentId: '__WORKSPACE_ID__',
604634
name: url || `cURL Import ${index + 1}`,
605-
parameters: [],
635+
parameters,
606636
url,
607637
method: (cmd.method || 'GET').toUpperCase(),
608638
headers,

0 commit comments

Comments
 (0)