Monday, 16 December 2019

Node js Fetch send empty body

Hi, I'm creating a rest api, I did everything right on the server, when I try to use fetch, always say "count" is not set, I try to give console.log (req.body) on the server and I always get {}Server sideapp.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 authorizationtoken." });​//if (bot.auth !== auth) return res.json({ "success": "false", "error": "Incorrectauthorization token." });bot.servers = count;res.json({ "success": "true", "bot": bot });});​Client side -Node js code​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)})});​The errors that I get[client side][1]​​[server side][2]​​[1]: https://i.stack.imgur.com/2kVgQ.png[2]: https://i.stack.imgur.com/tWEHe.png

Submitted December 17, 2019 at 01:23AM by Brubrunin

No comments:

Post a Comment