Skip to content

Commit 0d333dc

Browse files
committed
properly read file data in multipart form request type in cli
1 parent 5c0187b commit 0d333dc

5 files changed

Lines changed: 37 additions & 19 deletions

File tree

packages/flowtest-cli/bin/index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const argv = yargs(hideBin(process.argv))
6868
try {
6969
const flowData = serialize(JSON.parse(content));
7070
// output json output to a file
71-
//console.log(chalk.green(JSON.stringify(flowData)));
71+
7272
const logger = new GraphLogger();
7373
const startTime = Date.now();
7474
const g = new Graph(
@@ -80,13 +80,11 @@ const argv = yargs(hideBin(process.argv))
8080
logger,
8181
);
8282
console.log(chalk.yellow('Running Flow \n'));
83-
if (flowData.nodes.find((n) => n.type === 'flowNode')) {
84-
console.log(
85-
chalk.blue(
86-
'[Note] This flow contains nested flows so run it from root directory of collection. Ignore if already doing that. \n',
87-
),
88-
);
89-
}
83+
console.log(
84+
chalk.blue(
85+
'Right now CLI commands must be run from root directory of collection. We will gradually add support to run commands from anywhere inside the collection. \n',
86+
),
87+
);
9088
const result = await g.run();
9189
console.log('\n');
9290
if (result.status === 'Success') {

packages/flowtest-cli/graph/compute/requestnode.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ const Node = require('./node');
33
const axios = require('axios');
44
const chalk = require('chalk');
55
const { LogLevel } = require('../GraphLogger');
6+
const FormData = require('form-data');
7+
const { extend, cloneDeep } = require('lodash');
8+
const fs = require('fs');
9+
const path = require('path');
610

711
const newAbortSignal = () => {
812
const abortController = new AbortController();
@@ -113,19 +117,16 @@ class requestNode extends Node {
113117
: JSON.parse('{}');
114118
} else if (this.nodeData.requestBody.type === 'form-data') {
115119
contentType = 'multipart/form-data';
116-
requestData = {
117-
key: computeVariables(this.nodeData.requestBody.body.key, variablesDict),
118-
value: this.nodeData.requestBody.body.value,
119-
name: this.nodeData.requestBody.body.name,
120-
};
120+
const params = cloneDeep(this.nodeData.requestBody.body);
121+
requestData = params;
121122
}
122123
}
123124

124125
const options = {
125126
method: restMethod,
126127
url: finalUrl,
127128
headers: {
128-
'Content-type': contentType,
129+
'content-type': contentType,
129130
},
130131
data: requestData,
131132
};
@@ -141,12 +142,23 @@ class requestNode extends Node {
141142

142143
async runHttpRequest(request) {
143144
try {
144-
if (request.headers['Content-type'] === 'multipart/form-data') {
145-
const requestData = new FormData();
146-
const file = await convertBase64ToBlob(request.data.value);
147-
requestData.append(request.data.key, file, request.data.name);
145+
if (request.headers['content-type'] === 'multipart/form-data') {
146+
const formData = new FormData();
147+
const params = request.data;
148+
await params.map(async (param, index) => {
149+
if (param.type === 'text') {
150+
formData.append(param.key, param.value);
151+
}
152+
153+
if (param.type === 'file') {
154+
let trimmedFilePath = param.value.trim();
155+
156+
formData.append(param.key, fs.createReadStream(trimmedFilePath), path.basename(trimmedFilePath));
157+
}
158+
});
148159

149-
request.data = requestData;
160+
request.data = formData;
161+
extend(request.headers, formData.getHeaders());
150162
}
151163

152164
// assuming 'application/json' type

packages/flowtest-cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"boxen": "^7.1.1",
1818
"chalk": "^3.0.0",
1919
"dotenv": "^16.4.5",
20+
"form-data": "^4.0.0",
2021
"fs": "^0.0.1-security",
2122
"lodash": "^4.17.21",
2223
"omelette": "^0.4.17",

packages/flowtest-electron/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"dotenv": "^16.4.5",
4444
"electron-store": "^8.1.0",
4545
"flatted": "^3.3.1",
46+
"form-data": "^4.0.0",
4647
"fs": "^0.0.1-security",
4748
"json-refs": "^3.0.15",
4849
"langchain": "^0.1.28",

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)