我試圖用使一個POST請求節點/ HTTPS,但我不斷收到此錯誤:節點HTTPS請求 - MalformedJsonException
BODY: {"message":"MalformedJsonException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 11 path $","mdcKey":""}
這裏的發起請求的代碼:
const https = require('https');
const querystring = require('querystring');
const POSTData = querystring.stringify({
'addressTo': '[email protected]',
'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。我錯過了什麼?
修復它,謝謝你的答案! – timothym