我有一个奇怪的问题。 大多数情况下,下面的代码有效:
function invokeMauticAPI(requestProperties) {
let caller = saRequest[requestProperties.method.toLowerCase()](requestProperties.url);
caller = caller.set("Authorization", "Basic " + generateMauticAuthToken());
if (requestProperties.body !== undefined && requestProperties.body !== null) {
caller = caller.send(requestProperties.body);
} else {
caller = caller.send();
}
return new Promise((resolve, reject) => {
caller.end((err, response) => {
if (err) {
if (response && !err.response) {
err.response = response;
}
return reject(err);
}
if (response.ok) {
//creepy case made by mautic which is surely NOT very REST-full
if (response.body && response.body.success === false) {
let errObj = new Error(`Error response from mautic with status ${response.status} `);
errObj.details = response.body;
reject(errObj);
}
return resolve(response.body);
}
let errObj = new Error(`Error response from mautic with status ${response.status} `);
errObj.response = response;
return reject(errObj);
})
});
}
使用这样的请求设置调用代码(为安全起见替换了一些数据):
{
"method": "POST",
"url": "https://mautic.example.com/api/emails/31/contact/705/send",
"body": {
"tokens": {
"{userName}": "Kote",
"{siteUrl}": "https://dev.example.com/staging/",
"{messageText}": "Hello world",
"{detailsUrl}": "https://dev-local.example.com/staging/message/1234ABCD"
}
}
}
在mautic上,随机返回奇怪的错误响应而不是成功,并且在发生这种情况时未发送电子邮件:
{"success":false,"failed":[]}
风行版本为2.15.2 造成这种行为的原因是什么?