【发布时间】:2021-03-20 10:00:12
【问题描述】:
我正在尝试使用节点/HTTPS 发出 POST 请求,但我不断收到此错误:
BODY: {"message":"MalformedJsonException: com.google.gson.stream.MalformedJsonException:使用 JsonReader.setLenient(true) 在第 1 列接受格式错误的 JSON 11 路径 $","mdcKey":""}
下面是发起请求的代码:
const https = require('https');
const querystring = require('querystring');
const POSTData = querystring.stringify({
'addressTo': 'myemail@address.com',
'subject': 'testing your email template',
'templateName': 'genericTemplate',
'bodyText': 'this is a test'
});
console.log(POSTData);
const HTTPOptions = {
hostname: 'url.com',
port: 00000,
path: '/path',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const HTTPSrequest = https.request(HTTPOptions, (response) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
response.setEncoding('utf8');
response.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
response.on('end', () => {
console.log('No more data in response.');
});
});
HTTPSrequest.on('error', (error) => {
console.log(`problem with request: ${error}`);
});
// write data to request body
HTTPSrequest.write(POSTData);
HTTPSrequest.end();
我假设 POSTData 是“格式错误的 JSON”,这就是它看起来像字符串化的样子:
addressTo=name%40companyname.com&subject=testing%20your%20email%20template&templateName=genericTemplate&bodyText=this%20is%20a%20test
我不确定我在做什么会导致格式错误的 JSON。我错过了什么?
【问题讨论】:
标签: node.js