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
12 changes: 10 additions & 2 deletions packages/bruno-app/src/utils/curl/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { forOwn } from 'lodash';
import curlToJson from './curl-to-json';
import { prettifyJsonString } from 'utils/common/index';
import { prettifyJsonString, uuid } from 'utils/common/index';
import { isJsonLikeContentType, isPlainTextContentType, isXmlLikeContentType } from './content-type';

export const getRequestFromCurlCommand = (curlCommand, requestType = 'http-request') => {
Expand All @@ -13,6 +13,14 @@ export const getRequestFromCurlCommand = (curlCommand, requestType = 'http-reque
return formData;
};

const parseMultipartForm = (parsedBody) => {
if (!Array.isArray(parsedBody)) {
return [];
}

return parsedBody.map((param) => ({ uid: uuid(), ...param }));
};

const parseGraphQL = (text) => {
try {
const graphql = JSON.parse(text);
Expand Down Expand Up @@ -82,7 +90,7 @@ export const getRequestFromCurlCommand = (curlCommand, requestType = 'http-reque
body.formUrlEncoded = parseFormData(parsedBody);
} else if (normalizedContentType.includes('multipart/form-data')) {
body.mode = 'multipartForm';
body.multipartForm = parsedBody;
body.multipartForm = parseMultipartForm(parsedBody);
} else if (isPlainTextContentType(contentType)) {
body.mode = 'text';
body.text = parsedBody;
Expand Down
12 changes: 12 additions & 0 deletions packages/bruno-app/src/utils/curl/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ describe('getRequestFromCurlCommand', () => {
expect(Array.isArray(request.body.file)).toBe(true);
expect(request.body.file[0].filePath).toBe('/path/to/payload.json');
});

it('should map postman form-data into a multipart body', () => {
const curl = 'curl --location \'https://example.com/upload\' --form \'name="John"\' --form \'file=@"/path/to/file.txt"\'';

const request = getRequestFromCurlCommand(curl);

expect(request.body.mode).toBe('multipartForm');
expect(request.body.multipartForm).toEqual([
{ uid: expect.any(String), name: 'name', value: 'John', type: 'text', enabled: true },
{ uid: expect.any(String), name: 'file', value: ['/path/to/file.txt'], type: 'file', enabled: true }
]);
});
});
24 changes: 17 additions & 7 deletions packages/bruno-app/src/utils/curl/parse-curl.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,29 @@ const setFormData = (request, value) => {
/**
* Parse a single form field
* Handles text fields, quoted values, and file uploads (@path)
* Postman quotes every form value, eg: name="john" / file=@"/path/to/file.png"
*/
const parseFormField = (field) => {
const match = field.match(/^([^=]+)=(?:@?"([^"]*)"|@([^@]*)|([^@]*))?$/);
if (typeof field !== 'string') return null;

if (!match) return null;
const separatorIndex = field.indexOf('=');
if (separatorIndex < 0) return null;

const fieldName = match[1];
const fieldValue = match[2] || match[3] || match[4] || '';
const isFile = field.includes('@');
const name = field.slice(0, separatorIndex);
let value = field.slice(separatorIndex + 1);

// only a leading `@` marks a file upload, an `@` inside the value (eg an email) does not
const isFile = value.startsWith('@');
if (isFile) {
value = value.slice(1);
}

value = value.replace(/^"([\s\S]*)"$/, '$1');

return {
name: fieldName,
value: fieldValue,
name,
// multipart file params hold a list of file paths
value: isFile ? [value] : value,
type: isFile ? 'file' : 'text',
enabled: true
};
Expand Down
22 changes: 21 additions & 1 deletion packages/bruno-app/src/utils/curl/parse-curl.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,32 @@ describe('parseCurlCommand', () => {
expect(result).toEqual({
method: 'post',
multipartUploads: [
{ name: 'file', value: '/path/to/file.txt', type: 'file', enabled: true }
{ name: 'file', value: ['/path/to/file.txt'], type: 'file', enabled: true }
],
url: 'https://api.example.com/upload',
urlWithoutQuery: 'https://api.example.com/upload'
});
});

it('should parse the quoted form data postman generates', () => {
const result = parseCurlCommand(
'curl --location \'https://api.example.com/upload\' --form \'name="John"\' --form \'email="john@example.com"\' --form \'file=@"/path/to/file.txt"\''
);

expect(result.multipartUploads).toEqual([
{ name: 'name', value: 'John', type: 'text', enabled: true },
{ name: 'email', value: 'john@example.com', type: 'text', enabled: true },
{ name: 'file', value: ['/path/to/file.txt'], type: 'file', enabled: true }
]);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

it('should skip form fields without a name=value pair', () => {
const result = parseCurlCommand(`
curl --form "invalid" https://api.example.com/upload
`);

expect(result.multipartUploads).toEqual([]);
});
});

describe('Cookie', () => {
Expand Down