Skip to content

Commit 6b85ba2

Browse files
erunionreynolek
andauthored
fix: not sending form urlencoded properly in JS fetch snippets (#218)
Co-authored-by: Eric Reynolds <[email protected]>
1 parent 9530d5b commit 6b85ba2

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/targets/javascript/fetch.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,17 @@ module.exports = function (source, options) {
6767
}
6868
}
6969

70-
code.push('const options = %s;', stringifyObject(options, { indent: opts.indent, inlineCharacterLimit: 80 }))
70+
code.push('const options = %s;', stringifyObject(options, {
71+
indent: opts.indent,
72+
inlineCharacterLimit: 80,
73+
transform: (object, property, originalResult) => {
74+
if (property === 'body' && source.postData.mimeType === 'application/x-www-form-urlencoded') {
75+
return `new URLSearchParams(${originalResult})`
76+
}
77+
78+
return originalResult
79+
}
80+
}))
7181
.blank()
7282

7383
if (source.postData.mimeType === 'multipart/form-data') {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const options = {
22
method: 'POST',
33
headers: {'content-type': 'application/x-www-form-urlencoded'},
4-
body: {foo: 'bar', hello: 'world'}
4+
body: new URLSearchParams({foo: 'bar', hello: 'world'})
55
};
66

77
fetch('http://mockbin.com/har', options)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const options = {
55
accept: 'application/json',
66
'content-type': 'application/x-www-form-urlencoded'
77
},
8-
body: {foo: 'bar'}
8+
body: new URLSearchParams({foo: 'bar'})
99
};
1010

1111
fetch('http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value', options)

0 commit comments

Comments
 (0)