【问题标题】:How to set content-type in node-fetch如何在 node-fetch 中设置内容类型
【发布时间】:2021-08-30 13:21:42
【问题描述】:

这是我的代码:

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);
        })
    });

目前它不会使用内容类型 application/json ,我不知道为什么。有人可以帮忙吗?

【问题讨论】:

    标签: node.js node-fetch


    【解决方案1】:

    我认为你需要stringifybody 对象

    这是更新后的代码:

    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))
    

    您可以阅读有关feach api的详细信息here

    【讨论】:

    • 非常感谢! @Foyez
    猜你喜欢
    • 2016-09-28
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    相关资源
    最近更新 更多