From f110d898f5ad450d283d1c7e4978a86189f7c3aa Mon Sep 17 00:00:00 2001 From: Sanjai Kumar Date: Tue, 3 Dec 2024 18:01:22 +0530 Subject: [PATCH 1/2] fix: refactor createFormData function --- .../src/ipc/network/prepare-request.js | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/packages/bruno-electron/src/ipc/network/prepare-request.js b/packages/bruno-electron/src/ipc/network/prepare-request.js index c8b36bb8..80c3ee64 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-request.js @@ -1,5 +1,5 @@ const os = require('os'); -const { get, each, filter, compact, forOwn } = require('lodash'); +const { get, each, filter, compact, forEach } = require('lodash'); const decomment = require('decomment'); const FormData = require('form-data'); const fs = require('fs'); @@ -166,26 +166,34 @@ const mergeFolderLevelScripts = (request, requestTreePath, scriptFlow) => { } }; -const createFormData = (datas, collectionPath) => { +const createFormData = (data, collectionPath) => { // make axios work in node using form data // reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427 const form = new FormData(); - forOwn(datas, (value, key) => { - if (typeof value == 'string') { - form.append(key, value); + forEach(data, (datum) => { + const { name, type, value } = datum; + + if (type === 'text') { + if (Array.isArray(value)) { + value.forEach((val) => form.append(name, val)); + } else { + form.append(name, value); + } return; } - const filePaths = value || []; - filePaths?.forEach?.((filePath) => { - let trimmedFilePath = filePath.trim(); + if (type === 'file') { + const filePaths = value || []; + filePaths.forEach((filePath) => { + let trimmedFilePath = filePath.trim(); - if (!path.isAbsolute(trimmedFilePath)) { - trimmedFilePath = path.join(collectionPath, trimmedFilePath); - } + if (!path.isAbsolute(trimmedFilePath)) { + trimmedFilePath = path.join(collectionPath, trimmedFilePath); + } - form.append(key, fs.createReadStream(trimmedFilePath), path.basename(trimmedFilePath)); - }); + form.append(name, fs.createReadStream(trimmedFilePath), path.basename(trimmedFilePath)); + }); + } }); return form; }; @@ -435,11 +443,11 @@ const prepareRequest = (item, collection) => { } if (request.body.mode === 'multipartForm') { - axiosRequest.headers['content-type'] = 'multipart/form-data'; - const params = {}; + if (!contentTypeDefined) { + axiosRequest.headers['content-type'] = 'multipart/form-data'; + } const enabledParams = filter(request.body.multipartForm, (p) => p.enabled); - each(enabledParams, (p) => (params[p.name] = p.value)); - axiosRequest.data = params; + axiosRequest.data = createFormData(enabledParams); } if (request.body.mode === 'graphql') { From f1e6d5eefe2a2c8580b3d4c2f0f2a4fd0258ac24 Mon Sep 17 00:00:00 2001 From: lohit Date: Sun, 15 Dec 2024 17:38:22 +0530 Subject: [PATCH 2/2] add createFormData import from utils --- packages/bruno-electron/src/ipc/network/prepare-request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bruno-electron/src/ipc/network/prepare-request.js b/packages/bruno-electron/src/ipc/network/prepare-request.js index 5c82da40..de6ed163 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-request.js @@ -2,7 +2,7 @@ const { get, each, filter } = require('lodash'); const decomment = require('decomment'); const crypto = require('node:crypto'); const { getTreePathFromCollectionToItem, mergeHeaders, mergeScripts, mergeVars } = require('../../utils/collection'); -const { buildFormUrlEncodedPayload } = require('../../utils/form-data'); +const { buildFormUrlEncodedPayload, createFormData } = require('../../utils/form-data'); const setAuthHeaders = (axiosRequest, request, collectionRoot) => { const collectionAuth = get(collectionRoot, 'request.auth');