Atozed Forums
Upload media to WhatsApp cloud API - Printable Version

+- Atozed Forums (https://www.atozed.com/forums)
+-- Forum: Indy (https://www.atozed.com/forums/forum-8.html)
+--- Forum: Indy General Discussion (https://www.atozed.com/forums/forum-9.html)
+--- Thread: Upload media to WhatsApp cloud API (/thread-2969.html)



Upload media to WhatsApp cloud API - Lior I - 12-18-2022

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


RE: Upload media to WhatsApp cloud API - 3ddark - 12-19-2022

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.


RE: Upload media to WhatsApp cloud API - rlebeau - 12-19-2022

(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;



RE: Upload media to WhatsApp cloud API - Lior I - 12-26-2022

Thank you Remy Lebeau. Problem solved.