send file to whatsapp cloud api using the media endpoints

38 views Asked by At

Im using the whatsapp api to generate a file and send it to whatsapp api. I first test all parameters by postman and everything was great, postman generated this code:

const myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer token");
myHeaders.append("Cookie", "ps_l=0; ps_n=0");

const formdata = new FormData();
formdata.append("file", fileInput.files[0], "/C:/Users/Sub2/Documents/Hoja de trabajo.docx");
formdata.append("type", "application/vnd.ms-excel");
formdata.append("messaging_product", "whatsapp");

const requestOptions = {
  method: "POST",
  headers: myHeaders,
  body: formdata,
  redirect: "follow"
};

fetch("https://graph.facebook.com/v19.0/{{media_id}}/media", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));

If you use this code it will upload the file to the whatsapp api. The problem is that in node js its giving an error: {"error":{"message":"(#100) The parameter messaging_product is required.","type":"OAuthException","code":100,"fbtrace_id":"AAql5W804gIHMiKVCxX7InY"}}

The parameter messaging_product is required, but this code: formdata.append("messaging_product", "whatsapp"); add that parameter, i dont know what's the problem. The only code part that is different in my node code is this formdata.append("file", fileStream); im using the exceljs to generate a file, Thank you for any suggestions.

Update: This code is not working:

const myHeaders = new Headers();
    myHeaders.append("Authorization", "Bearer token");
    myHeaders.append("Cookie", "ps_l=0; ps_n=0");

    const formdata = new FormData();
    formdata.append("file", fileBuffer);
    formdata.append("type", "application/vnd.ms-excel");
    formdata.append("messaging_product", "whatsapp");

    const requestOptions = {
        method: "POST",
        headers: myHeaders,
        body: formdata,
        redirect: "follow"
    };

    fetch("https://graph.facebook.com/v19.0/{{media_id}}/media", requestOptions)
    .then((response) => response.text())
    .then((result) => console.log(result))
    .catch((error) => console.error(error))

Update 2 I resolve the problem installing axios and just copy and paste the code generated by postman, thank you everyone for the help and advices

0

There are 0 answers