Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda