diff --git a/packages/bruno-cli/src/runner/prepare-request.js b/packages/bruno-cli/src/runner/prepare-request.js index 567087f3..21cf17bd 100644 --- a/packages/bruno-cli/src/runner/prepare-request.js +++ b/packages/bruno-cli/src/runner/prepare-request.js @@ -2,35 +2,7 @@ const { get, each, filter } = require('lodash'); const decomment = require('decomment'); const crypto = require('node:crypto'); const { mergeHeaders, mergeScripts, mergeVars, getTreePathFromCollectionToItem } = require('../utils/collection'); - -const createFormData = (datas, collectionPath) => { - // make axios work in node using form data - // reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427 - const form = new FormData(); - datas.forEach((item) => { - const value = item.value; - const name = item.name; - let options = {}; - if (item.contentType) { - options.contentType = item.contentType; - } - if (item.type === 'file') { - const filePaths = value || []; - filePaths.forEach((filePath) => { - let trimmedFilePath = filePath.trim(); - - if (!path.isAbsolute(trimmedFilePath)) { - trimmedFilePath = path.join(collectionPath, trimmedFilePath); - } - options.filename = path.basename(trimmedFilePath); - form.append(name, fs.createReadStream(trimmedFilePath), options); - }); - } else { - form.append(name, value, options); - } - }); - return form; -}; +const { createFormData } = require('../utils/form-data'); const prepareRequest = (item = {}, collection = {}) => { const request = item?.request; @@ -164,7 +136,8 @@ const prepareRequest = (item = {}, collection = {}) => { if (request.body.mode === 'multipartForm') { axiosRequest.headers['content-type'] = 'multipart/form-data'; const enabledParams = filter(request.body.multipartForm, (p) => p.enabled); - axiosRequest.data = createFormData(enabledParams); + const collectionPath = process.cwd(); + axiosRequest.data = createFormData(enabledParams, collectionPath); } if (request.body.mode === 'graphql') { diff --git a/packages/bruno-cli/src/utils/form-data.js b/packages/bruno-cli/src/utils/form-data.js new file mode 100644 index 00000000..f5eec945 --- /dev/null +++ b/packages/bruno-cli/src/utils/form-data.js @@ -0,0 +1,40 @@ +const { forEach } = require('lodash'); +const FormData = require('form-data'); +const fs = require('fs'); +const path = require('path'); + +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(); + 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; + } + + if (type === 'file') { + const filePaths = value || []; + filePaths.forEach((filePath) => { + let trimmedFilePath = filePath.trim(); + console.log(trimmedFilePath, collectionPath); + if (!path.isAbsolute(trimmedFilePath)) { + trimmedFilePath = path.join(collectionPath, trimmedFilePath); + } + + form.append(name, fs.createReadStream(trimmedFilePath), path.basename(trimmedFilePath)); + }); + } + }); + return form; +}; + +module.exports = { + createFormData +} \ No newline at end of file diff --git a/packages/bruno-electron/src/utils/form-data.js b/packages/bruno-electron/src/utils/form-data.js index cb799682..81a3c562 100644 --- a/packages/bruno-electron/src/utils/form-data.js +++ b/packages/bruno-electron/src/utils/form-data.js @@ -1,4 +1,4 @@ -const { forOwn } = require('lodash'); +const { forEach } = require('lodash'); const FormData = require('form-data'); const fs = require('fs'); const path = require('path');