Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Upload media to WhatsApp cloud API
#1
I need to upload a PDF file to a WhatsApp Cloud API.
Can someone help me translate this WhatsApp curl example code to Delphi pascal?
Here is the code from the WhatsApp documentation (JPEG example):
Code:
curl -X POST \
  'https://graph.facebook.com/v15.0/FROM_PHONE_NUMBER_ID/media' \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -F 'file=@/local/path/file.jpg;type=image/jpeg'
  -F 'messaging_product=whatsapp'

I don't know how to translate the file parameter. It includes both path and file type attributes.
Code:
-F 'file=@/local/path/file.jpg;type=image/jpeg' 


I use Delphi RIO 10.3
Thanks in advance
Reply
#2
This example http/https post. You can do this using the Indy IdHttp or Delphi NetHTTPClient component.

Post example with link
Add Request header for Authorization

If you want to know how the structure works, you should research what is HTTP/https request/response.
Reply
#3
(12-18-2022, 10:24 AM)Lior I Wrote: Can someone help me translate this WhatsApp curl example code to Delphi pascal?
...
I don't know how to translate the file parameter. It includes both path and file type attributes.
Code:
-F 'file=@/local/path/file.jpg;type=image/jpeg' 

If you read curl's documentation, you would see that the -F parameter is for sending an HTML webform in multipart/form-data format. Indy's TIdHTTP has an overloaded Post() method which takes a TIdMultipartFormDataStream as input, eg:

Code:
uses
  ..., IdMultipartFormDataStream;

var
  PostData: TIdMultipartFormDataStream;
begin
  PostData := TIdMultipartFormDataStream.Create;
  try
    PostData.AddFile('file', '/local/path/file.jpg', 'image/jpeg');
    PostData.AddFormField('messaging_product', 'whatsapp');

    IdHTTP.Request.CustomHeaders.Values['Authorization'] := 'Bearer ACCESS_TOKEN';
    IdHTTP.Request.BasicAuthentication := False;

    IdHTTP.Post('https://graph.facebook.com/v15.0/FROM_PHONE_NUMBER_ID/media', PostData);
  finally
    PostData.Free;
  end;
end;

Reply
#4
Thank you Remy Lebeau. Problem solved.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)