简体   繁体   English

如何在 node-fetch 中设置内容类型

[英]How to set content-type in node-fetch

this is my code:这是我的代码:

return fetch("https://www.motiondevelopment.top/api/v1.2/bots/" + botid + "/stats", {
        method: "post",
        headers: { 
            "Content-Type": "application/json",
            "key": apikey,
        },
        body: { "guilds": serverNumber }
    }).then((response) => {
        return response.json().then((data) => {
            return data;
        }).catch((err) => {
            console.log("[MotionBotList]: " + err);
        })
    });

At the moment it will not use the content-type application/json and I don't know why.目前它不会使用内容类型 application/json ,我不知道为什么。 Could someone help please?有人可以帮忙吗?

I think you need to stringify body object .我认为您需要对正文 object进行stringify

here is the updated code:这是更新的代码:

fetch("https://www.motiondevelopment.top/api/v1.2/bots/" + botid + "/stats", {
  method: "post",
  headers: { 
    "Content-Type": "application/json",
    "key": apikey,
  },
  body: JSON.stringify({ "guilds": serverNumber })
})
  .then(res => res.json())
  .then(data => console.log(data))
  .catch(err => console.log(err))

you can read details about feach api here您可以 在此处阅读有关feach api的详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM