Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
Having a problem properly getting information from canvas and sending information to discord through webhook with node-fetch

So I am trying to send data from canvas's api using a GET and use that information and send a POST from the same endpoint to discord using node fetch. I can receive data from canvas without issue and I console log to make sure I have to right data, but I can't seem to get any information to discord. I am using discords webhooks and I can't figure out where I am going wrong.

fetch(url + `courses/${course}/discussion_topics` , {
    method: "GET",
    headers : {
        'Authorization' : 'Bearer <auth token>',
        'Content-Type' : 'application/json'
    }
    
})
.then(res => res.json())
.then(data => { 
       
    console.log(data[0].id);
    console.log(data[0].title);
    console.log(data[0].message);
}
 ) 
  .then(fetch("https://discord.com/api/webhooks/893327519103746149/<webhooktoken>", {
    method: "post",
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
   body: {content: 'hello world'}
    
}))
.catch(err => console.log(err))

});```
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As mentioned in the comment, just in case you have some typo or misunderstanding.

Also, you need to JSON.stringyify your body.

Please try the example below:

fetch(url + `courses/${course}/discussion_topics`, {
  method: "GET",
  headers: {
    Authorization: "Bearer <auth token>",
    "Content-Type": "application/json",
  },
})
  .then(res => res.json())
  .then(data => {
    console.log(data[0].id);
    console.log(data[0].title);
    console.log(data[0].message);
  })
  .then(() =>
    fetch(
      "https://discord.com/api/webhooks/893327519103746149/<webhooktoken>",
      {
        method: "POST",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          username: "Canvas-Bot",
          content: "hello world",
        }),
      }
    )
      .then(res => res.json())
      .then(data => {
        console.log({ data });
      })
  )
  .catch(err => console.log(err));

Another approach would be in async/await. I think it is cleaner.

(async function main() {
  try {
    const res1 = await fetch(url + `courses/${course}/discussion_topics`, {
      method: "GET",
      headers: {
        Authorization: "Bearer <auth token>",
        "Content-Type": "application/json",
      },
    });
    const data1 = await res1.json();
    console.log(data1);

    const res2 = await fetch(
      "https://discord.com/api/webhooks/893327519103746149/<webhooktoken>",
      {
        method: "POST",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          username: "Canvas-Bot",
          content: "hello world",
        }),
      }
    );

    const data2 = await res2.json();
    console.log(data2);
  } catch (err) {
    console.log(err);
  }
})();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda