Skip to content

Commit 7ef78f4

Browse files
committed
[Kong#11][feature] Fixed issue with multipart-form
1 parent 62a01ed commit 7ef78f4

2 files changed

Lines changed: 25 additions & 17 deletions

File tree

‎src/targets/node/request.js‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,30 @@ module.exports = function(options) {
3232
}
3333
reqOpts.json = true;
3434
}
35-
else if(this.source.headersObj["Content-Type"] === "multipart/form-data"){
36-
this.source.postData.params.forEach(function(param){
35+
else if (this.source.headersObj["Content-Type"] === "multipart/form-data") {
36+
this.source.postData.params.forEach(function(param) {
3737
reqOpts.formData = reqOpts.formData || {};
3838
reqOpts.formData[param.name] = {};
39-
if (!param.value.length){
40-
param.value = 'fs.createReadStream(\''+param.fileName+'\')';
41-
fsReplace.push(param.value);
42-
reqOpts.formData[param.name].value =param.value;
43-
if(param.fileName.indexOf('/')>-1){
39+
if (param.fileName) {
40+
if (!param.value.length) {
41+
param.value = 'fs.createReadStream(\'' + param.fileName + '\')';
42+
fsReplace.push(param.value);
43+
}
44+
reqOpts.formData[param.name].value = param.value;
45+
if (param.fileName.indexOf('/') > -1) {
4446
param.fileName = param.fileName.split('/');
45-
param.fileName = param.fileName[param.fileName.length-1];
47+
param.fileName = param.fileName[param.fileName.length - 1];
4648
}
47-
else if(param.fileName.indexOf('\\')>-1){
49+
else if (param.fileName.indexOf('\\') > -1) {
4850
param.fileName = param.fileName.split('\\');
49-
param.fileName = param.fileName[param.fileName.length-1];
51+
param.fileName = param.fileName[param.fileName.length - 1];
5052
}
5153
reqOpts.formData[param.name].options = {
52-
filename:param.fileName,
53-
"content-type":param.contentType
54+
filename: param.fileName,
55+
"content-type": param.contentType
5456
}
5557
}
56-
else{
58+
else {
5759
reqOpts.formData[param.name] = param.value;
5860
}
5961
})
@@ -73,15 +75,15 @@ module.exports = function(options) {
7375
}
7476

7577
code.push('var request = require(\'request\');');
76-
if(fsReplace.length>0){
78+
if (fsReplace.length > 0) {
7779
code.push('var fs = require(\'fs\');');
7880
}
7981

8082
code.push(null);
8183

8284
var options = !simpleRequest ? util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent)) : null;
83-
fsReplace.forEach(function(fsToReplace){
84-
options = options.replace('"'+fsToReplace+'"',fsToReplace);
85+
fsReplace.forEach(function(fsToReplace) {
86+
options = options.replace('"' + fsToReplace + '"', fsToReplace);
8587
})
8688
code.push(options);
8789

‎test/fixtures/output/node/request/multipart-file.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ var options = {
66
"Content-Type": "multipart/form-data"
77
},
88
"formData": {
9-
"foo": "hello world"
9+
"foo": {
10+
"value": "hello world",
11+
"options": {
12+
"filename": "hello.txt",
13+
"content-type": "text/plain"
14+
}
15+
}
1016
}
1117
};
1218

0 commit comments

Comments
 (0)