最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Whatsapp Cloud API uploading media files error - Stack Overflow

programmeradmin1浏览0评论

currently I am trying to make a thin wrapper around Whatsapp Cloud API. One of the problem I am currently facing is that the uploading media files isn't working.

Looking through the documentation for uploading files, it seems that it expects a multipart/form-data payload to the endpoint.

Here is my current implementation

import FormData from 'form-data';
import fs from 'fs';
import axios from 'axios';

const formData = new FormData();

formData.append('file', fs.createReadStream('path/to/my/file.jpg'));
formData.append('messaging_product', 'whatsapp');
formData.append('type', 'image/jpeg');

await axios.post('.0/PHONE_NO_ID/media', formData, {
  headers: { 'Authorization': ACCESS_TOKEN }
});

Looking at the error that it returns, it seems that I am somehow missing the messaging_product field even though I have properly add it in the formData.

{
  "error": {
    "message": "(#100) The parameter messaging_product is required.",
    "type": "OAuthException",
    "code": 100,
    "fbtrace_id": "FBTRACE_ID"
  }
}

The Postman collection works for uploading media file, so I am thinking that the file field of the formData is the problem. Is fs.createReadStream on a file equivalent to how Postman handle file uploading

EDIT: The question has been solved, the problems was not adding in the headers generated by the formData, Thanks Phil below!

currently I am trying to make a thin wrapper around Whatsapp Cloud API. One of the problem I am currently facing is that the uploading media files isn't working.

Looking through the documentation for uploading files, it seems that it expects a multipart/form-data payload to the endpoint.

Here is my current implementation

import FormData from 'form-data';
import fs from 'fs';
import axios from 'axios';

const formData = new FormData();

formData.append('file', fs.createReadStream('path/to/my/file.jpg'));
formData.append('messaging_product', 'whatsapp');
formData.append('type', 'image/jpeg');

await axios.post('https://graph.facebook./v14.0/PHONE_NO_ID/media', formData, {
  headers: { 'Authorization': ACCESS_TOKEN }
});

Looking at the error that it returns, it seems that I am somehow missing the messaging_product field even though I have properly add it in the formData.

{
  "error": {
    "message": "(#100) The parameter messaging_product is required.",
    "type": "OAuthException",
    "code": 100,
    "fbtrace_id": "FBTRACE_ID"
  }
}

The Postman collection works for uploading media file, so I am thinking that the file field of the formData is the problem. Is fs.createReadStream on a file equivalent to how Postman handle file uploading

EDIT: The question has been solved, the problems was not adding in the headers generated by the formData, Thanks Phil below!

Share Improve this question edited Aug 11, 2022 at 2:28 Phil 165k25 gold badges262 silver badges267 bronze badges asked Aug 9, 2022 at 6:14 Yu Yue NgYu Yue Ng 831 silver badge5 bronze badges 4
  • have you added the authentication token in the header? – turivishal Commented Aug 9, 2022 at 10:02
  • Yes I did I am very sorry that I didn't make this clear, this is not a authentication issue as I tried sending a text message and it was working – Yu Yue Ng Commented Aug 10, 2022 at 1:12
  • this is not messaging_product parameter issue, this error message is a glitch in WA-API. there might be auth token or file selection issue. try to check in postman and use this predefined postman api. – turivishal Commented Aug 10, 2022 at 5:19
  • Hi, thanks for giving me that postman collection, I tried it and it works so this is most definitely something wrong with my code. I am not sure how postman handles file in the context of NodeJS, would opening a read stream be equivalent to what is postman is doing under the hood? – Yu Yue Ng Commented Aug 10, 2022 at 5:57
Add a ment  | 

2 Answers 2

Reset to default 8

Love the documentation... they say type is a required parameter then go on to show a curl example without it

发布评论

评论列表(0)

  1. 暂无评论