Search code examples
node.jsnode-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. Could someone help please?


Solution

  • I think you need to stringify body object.

    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