我正在创建一个REST API,我在服务器上执行了所有操作,当我尝试使用fetch时,总是说未设置“ count”,我尝试在服务器上提供console.log(req.body)而且我总是得到{}
app.post("/api/stats/:id", async (req, res) => {
let botId = req.params.id;
console.log(req.body);
let auth = req.headers.authorization;
if (!auth)
return res.json({
success: "false",
error: "Authorization header not found."
});
let count = req.body.count ? req.body.count : req.body.server_count;
if (!count) return res.json({ success: "false", error: "Count not found in body." });
count = parseInt(count);
if (!count) return res.json({ success: "false", error: "Count not integer." });
const bot = client.fetchUser(`${botId}`);
if (!bot) return res.json({ success: "false", error: "Bot not found." });
// if (!bot.auth) return res.json({ "success": "false", "error": "Create a bot authorization token." });
//if (bot.auth !== auth) return res.json({ "success": "false", "error": "Incorrect authorization token." });
bot.servers = count;
res.json({ success: "true", bot: bot });
});
客户端-Node js代码:
const body = { count: 1 };
fetch("https://www.cloudlist.xyz/api/stats/64492636015388263", {
method: "POST",
headers: {
Authorization: `MY AUTH`,
"Content-Type": "application/json"
},
body: JSON.stringify({ body })
})
.then(res => res.json())
.then(async response => {
console.log(response);
});
我得到的错误:
客户端
服务器端