Skip to content

Commit 07d5ebf

Browse files
fix: updating the node-fetch target to handle form-urlencoded requests (#187)
* fix: updating the node-fetch target to handle form-urlencoded requests * fix: supplying URLSearchParams directly to `node-fetch` Co-authored-by: Opender Singh <opender.singh@konghq.com>
1 parent 86e7b0e commit 07d5ebf

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

src/targets/node/fetch.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ module.exports = function (source, options) {
3737

3838
switch (source.postData.mimeType) {
3939
case 'application/x-www-form-urlencoded':
40-
reqOpts.body = source.postData.paramsObj
40+
code.unshift('const { URLSearchParams } = require(\'url\');')
41+
code.push('const encodedParams = new URLSearchParams();')
42+
code.blank()
43+
44+
source.postData.params.forEach(function (param) {
45+
code.push('encodedParams.set(\'' + param.name + '\', \'' + param.value + '\');')
46+
})
47+
48+
reqOpts.body = 'encodedParams'
4149
break
4250

4351
case 'application/json':
@@ -49,15 +57,16 @@ module.exports = function (source, options) {
4957
case 'multipart/form-data':
5058
code.unshift('const FormData = require(\'form-data\');')
5159
code.push('const formData = new FormData();')
60+
code.blank()
61+
5262
source.postData.params.forEach(function (param) {
5363
if (!param.fileName && !param.fileName && !param.contentType) {
54-
code.push('formData.append(\'' + param.name + '\',\'' + param.value + '\');')
64+
code.push('formData.append(\'' + param.name + '\', \'' + param.value + '\');')
5565
return
5666
}
5767

5868
if (param.fileName) {
5969
includeFS = true
60-
code.blank()
6170
code.push('formData.append(\'' + param.name + '\', fs.createReadStream(\'' + param.fileName + '\'));')
6271
}
6372
})
@@ -100,7 +109,9 @@ module.exports = function (source, options) {
100109
.push(1, '.then(json => console.log(json))')
101110
.push(1, '.catch(err => console.error(\'error:\' + err));')
102111

103-
return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")')
112+
return code.join()
113+
.replace(/'encodedParams'/, 'encodedParams')
114+
.replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")')
104115
}
105116

106117
module.exports.info = {

test/fixtures/output/node/fetch/application-form-encoded.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
const { URLSearchParams } = require('url');
12
const fetch = require('node-fetch');
3+
const encodedParams = new URLSearchParams();
4+
5+
encodedParams.set('foo', 'bar');
6+
encodedParams.set('hello', 'world');
27

38
let url = 'http://mockbin.com/har';
49

510
let options = {
611
method: 'POST',
712
headers: {'content-type': 'application/x-www-form-urlencoded'},
8-
body: {foo: 'bar', hello: 'world'}
13+
body: encodedParams
914
};
1015

1116
fetch(url, options)

test/fixtures/output/node/fetch/full.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
const { URLSearchParams } = require('url');
12
const fetch = require('node-fetch');
3+
const encodedParams = new URLSearchParams();
4+
5+
encodedParams.set('foo', 'bar');
26

37
let url = 'http://mockbin.com/har';
48

@@ -10,7 +14,7 @@ let options = {
1014
'content-type': 'application/x-www-form-urlencoded',
1115
cookie: 'foo=bar; bar=baz; '
1216
},
13-
body: {foo: 'bar'}
17+
body: encodedParams
1418
};
1519

1620
fetch(url, options)

test/fixtures/output/node/fetch/multipart-form-data.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const FormData = require('form-data');
22
const fetch = require('node-fetch');
33
const formData = new FormData();
4-
formData.append('foo','bar');
4+
5+
formData.append('foo', 'bar');
56

67
let url = 'http://mockbin.com/har';
78

0 commit comments

Comments
 (0)