Skip to content

Commit e2fa910

Browse files
authored
Encode to JSON body for js-fetch target (#179)
Closes #158
1 parent 55249d7 commit e2fa910

File tree

4 files changed

+4
-27
lines changed

4 files changed

+4
-27
lines changed

src/targets/javascript/fetch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports = function (source, options) {
4040
break
4141

4242
case 'application/json':
43-
options.body = source.postData.jsonObj
43+
options.body = JSON.stringify(source.postData.jsonObj)
4444
break
4545

4646
case 'multipart/form-data':

test/fixtures/output/javascript/fetch/application-json.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,7 @@ fetch("http://mockbin.com/har", {
33
"headers": {
44
"content-type": "application/json"
55
},
6-
"body": {
7-
"number": 1,
8-
"string": "f\"oo",
9-
"arr": [
10-
1,
11-
2,
12-
3
13-
],
14-
"nested": {
15-
"a": "b"
16-
},
17-
"arr_mix": [
18-
1,
19-
"a",
20-
{
21-
"arr_mix_nested": {}
22-
}
23-
],
24-
"boolean": false
25-
}
6+
"body": "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"
267
})
278
.then(response => {
289
console.log(response);

test/fixtures/output/javascript/fetch/jsonObj-multiline.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ fetch("http://mockbin.com/har", {
33
"headers": {
44
"content-type": "application/json"
55
},
6-
"body": {
7-
"foo": "bar"
8-
}
6+
"body": "{\"foo\":\"bar\"}"
97
})
108
.then(response => {
119
console.log(response);

test/fixtures/output/javascript/fetch/jsonObj-null-value.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ fetch("http://mockbin.com/har", {
33
"headers": {
44
"content-type": "application/json"
55
},
6-
"body": {
7-
"foo": null
8-
}
6+
"body": "{\"foo\":null}"
97
})
108
.then(response => {
119
console.log(response);

0 commit comments

Comments
 (0)