Así que estoy tratando de enviar datos desde la api de canvas usando un GET y uso esa información y envío un POST desde el mismo punto final a discord usando node fetch. Puedo recibir datos del lienzo sin problemas y registro en la consola para asegurarme de que tengo los datos correctos, pero parece que no puedo obtener ninguna información para discordar. Estoy usando webhooks de discords y no puedo entender dónde me estoy equivocando.
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)) });```Como se menciona en el comentario, en caso de que tenga algún error tipográfico o malentendido.
Además, necesita JSON.stringyify su cuerpo.
Por favor, pruebe el siguiente ejemplo:
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));Otro enfoque sería en async/await. Creo que es más limpio.
(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); } })();