Skip to content

Commit

Permalink
fix(code-generator): when is a file in a multipart form request when …
Browse files Browse the repository at this point in the history
…generate to curl map fileName property

fixes usebruno#2224
  • Loading branch information
Leonardo Ferreira Lima committed May 3, 2024
1 parent c17e4ef commit 7a6e7d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ const CodeView = ({ language, item }) => {
let snippet = '';

try {
snippet = new HTTPSnippet(buildHarRequest({ request: item.request, headers })).convert(target, client);
snippet = new HTTPSnippet(buildHarRequest({ request: item.request, headers, target, client })).convert(
target,
client
);
} catch (e) {
console.error(e);
snippet = 'Error generating code snippet';
Expand Down
20 changes: 16 additions & 4 deletions packages/bruno-app/src/utils/codegenerator/har.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,26 @@ const createQuery = (queryParams = []) => {
}));
};

const createPostData = (body) => {
const createPostData = (body, { target, client }) => {
const contentType = createContentType(body.mode);
if (body.mode === 'formUrlEncoded' || body.mode === 'multipartForm') {
return {
mimeType: contentType,
params: body[body.mode]
.filter((param) => param.enabled)
.map((param) => ({ name: param.name, value: param.value }))
.map(({ name, value, type }) => {
if (body.mode === 'multipartForm' && type === 'file' && target === 'shell' && client === 'curl') {
return {
name,
fileName: value
};
}

return {
name,
value
};
})
};
} else {
return {
Expand All @@ -48,15 +60,15 @@ const createPostData = (body) => {
}
};

export const buildHarRequest = ({ request, headers }) => {
export const buildHarRequest = ({ request, headers, target, client }) => {
return {
method: request.method,
url: encodeURI(request.url),
httpVersion: 'HTTP/1.1',
cookies: [],
headers: createHeaders(headers),
queryString: createQuery(request.params),
postData: createPostData(request.body),
postData: createPostData(request.body, { target, client }),
headersSize: 0,
bodySize: 0
};
Expand Down

0 comments on commit 7a6e7d8

Please sign in to comment.