Skip to content

Commit 7d09277

Browse files
committed
add cleaner node:native request bodies when possible
1 parent a207126 commit 7d09277

4 files changed

Lines changed: 27 additions & 9 deletions

File tree

src/targets/node/native.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ module.exports = function (source, options) {
3030

3131
code.push(util.format('var http = require("%s");', source.uriObj.protocol.replace(':', '')))
3232

33-
if (!source.postData.text && source.postData.params) {
34-
code.push('var querystring = require("querystring");')
35-
}
36-
3733
code.blank()
3834
.push(util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent)))
3935
.blank()
@@ -51,8 +47,24 @@ module.exports = function (source, options) {
5147
.push('});')
5248
.blank()
5349

54-
if (source.postData.text) {
55-
code.push(util.format('req.write(%s);', JSON.stringify(source.postData.text)))
50+
switch (source.postData.mimeType) {
51+
case 'application/x-www-form-urlencoded':
52+
if (source.postData.paramsObj) {
53+
code.unshift('var qs = require("querystring");')
54+
code.push(util.format('req.write(qs.stringify(%s));', util.inspect(source.postData.paramsObj)))
55+
}
56+
break
57+
58+
case 'application/json':
59+
if (source.postData.jsonObj) {
60+
code.push(util.format('req.write(JSON.stringify(%s));', util.inspect(source.postData.jsonObj)))
61+
}
62+
break
63+
64+
default:
65+
if (source.postData.text) {
66+
code.push(util.format('req.write(%s);', JSON.stringify(source.postData.text, null, opts.indent)))
67+
}
5668
}
5769

5870
code.push('req.end();')

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var qs = require("querystring");
12
var http = require("http");
23

34
var options = {
@@ -23,5 +24,5 @@ var req = http.request(options, function (res) {
2324
});
2425
});
2526

26-
req.write("foo=bar&hello=world");
27+
req.write(qs.stringify({ foo: 'bar', hello: 'world' }));
2728
req.end();

test/fixtures/output/node/native/application-json.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ var req = http.request(options, function (res) {
2323
});
2424
});
2525

26-
req.write("{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }");
26+
req.write(JSON.stringify({ number: 1,
27+
string: 'f"oo',
28+
arr: [ 1, 2, 3 ],
29+
nested: { a: 'b' },
30+
arr_mix: [ 1, 'a', { arr_mix_nested: {} } ] }));
2731
req.end();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var qs = require("querystring");
12
var http = require("http");
23

34
var options = {
@@ -25,5 +26,5 @@ var req = http.request(options, function (res) {
2526
});
2627
});
2728

28-
req.write("foo=bar");
29+
req.write(qs.stringify({ foo: 'bar' }));
2930
req.end();

0 commit comments

Comments
 (0)