Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.0.0+

WhatsApp - Upload Media

Demonstrates how to upload media to the WhatsApp Business API client. The POST body must contain the binary media data and the Content-Type header must be set to the type of the media being uploaded.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
http: TChilkatHttp;
bdRequestBody: TChilkatBinData;
resp: TChilkatHttpResponse;
sbResponseBody: TChilkatStringBuilder;
jResp: TChilkatJsonObject;
respStatusCode: Integer;
id: WideString;
i: Integer;
count_i: Integer;

begin
success := 0;

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

http := TChilkatHttp.Create(Self);

//  Implements the following CURL command:

//  curl -X POST \
//  https://your-webapp-hostname:your-webapp-port/v1/media \
//  -H 'Authorization: Bearer your-auth-token' \ 
//  -H 'Content-Type: image/jpeg'
//  --data-binary @your-file-path

//  Use the following online tool to generate HTTP code from a CURL command
//  Convert a cURL Command to HTTP Source Code

bdRequestBody := TChilkatBinData.Create(Self);
success := bdRequestBody.LoadFile('your-file-path');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Failed to load your-file-path');
    Exit;
  end;

//  Adds the "Authorization: Bearer your-auth-token" header.
http.AuthToken := 'your-auth-token';

resp := TChilkatHttpResponse.Create(Self);
success := http.HttpBd('POST','https://your-webapp-hostname:your-webapp-port/v1/media',bdRequestBody.ControlInterface,'image/jpeg',resp.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(http.LastErrorText);
    Exit;
  end;

sbResponseBody := TChilkatStringBuilder.Create(Self);
resp.GetBodySb(sbResponseBody.ControlInterface);
jResp := TChilkatJsonObject.Create(Self);
jResp.LoadSb(sbResponseBody.ControlInterface);
jResp.EmitCompact := 0;

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(jResp.Emit());

respStatusCode := resp.StatusCode;
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(resp.Header);
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

//  Sample JSON response:
//  (Sample code for parsing the JSON response is shown below)

//  {
//    "messages": [
//      {
//        "id": "gBEGkYiEB1VXAglK1ZEqA1YKPrU"
//      }
//    ]
//  }

//  Sample code for parsing the JSON response...
//  Use the following online tool to generate parsing code from sample JSON:
//  Generate Parsing Code from JSON

i := 0;
count_i := jResp.SizeOfArray('messages');
while i < count_i do
  begin
    jResp.I := i;
    id := jResp.StringOf('messages[i].id');
    i := i + 1;
  end;