【发布时间】:2017-09-25 10:09:21
【问题描述】:
我正在尝试使用以下代码在 nodejs 中执行 HTTPS REST 请求:
var querystring = require('querystring');
var https = require('https');
var postData = {
'Value1' : 'abc1',
'Value2' : 'abc2',
'Value3' : '3'
};
var postBody = querystring.stringify(postData);
var options = {
host: 'URL'
port: 443,
path: 'PATH'
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postBody.length
}
};
var req = https.request(options, function(res) {
console.log(res.statusCode);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.write(postBody);
req.end();
req.on('error', function(e) {
console.error(e);
});
请求有效,但不如预期。正文不会以 JSON 格式发送,看起来像:
RequestBody":"Value1=abc1&Value2=abc2&Value3=3
输出应该是这样的:
RequestBody":"[\r\n {\r\n \"Value3\": \"3\",\r\n \"Value2\": \"abc2\",\r\n \"Value1\": \"abc1\"\r\n }\r\n]
我认为它与stringify有关,也许我无论如何都必须将其转换为JSON格式..
【问题讨论】:
标签: javascript json node.js https restful-url