Vercel无服务器功能未运行axios post请求



我正在使用Vercel无服务器函数来运行对webhook的post请求。这在localhost上正常工作,但在部署到Vercel无服务器功能后不工作。

async function formSubmission(req, res) {
res.statusCode = 200;
console.log('form-submission-init');
axios({
method: 'POST',
url: 'https://flow.zoho.in/*',
data: req.body,
})
.then((response) => {
console.log('success');
})
.catch((error) => {
console.log('fail', error);
});
res.json({ data: 'done' });
}

Vercel日志form-submission-initVercel日志不打印failsuccess

我已经浏览了Vercel的文档,了解为什么它可能不起作用,链接但不确定。感谢您的帮助。

您有异步流问题,在实际解决axios承诺之前发送响应res.json

您要么需要等待axios请求,要么将res.json放入promise链中。

我最近遇到了这个问题,但我终于解决了。这是我的代码,我希望它能帮助你。

const axios = require('axios')
module.exports = async (req, res) => {
const json = req.body;
let output = parseData(json);
let data = {
msg: output
};
// webhook
if (output.startsWith("The price is")) {
let price = output.match(/d+.d+/)[0];
// Use await to wait for the webhook request to finish
try {
await sendWebhook(price);
console.log("Webhook request sent successfully!");
} catch (error) {
console.log("Error sending webhook request:", error);
}
}
// Send the response after the webhook request is resolved
res.status(200).json(data);
};
function sendWebhook(price) {
let msg_url = `http://api.xxxxx.app/Va5GhE/${price}`;
let config = {
method: 'post',
maxBodyLength: Infinity,
url: msg_url,
headers: {}
};
// Return the axios promise so that it can be awaited in the calling function
return axios.request(config);
}

最新更新