Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 DLL Downloads
var
success: Boolean;
http: HCkHttp;
bdRequestBody: HCkBinData;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
id: PWideChar;
i: Integer;
count_i: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := CkHttp_Create();
// 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 := CkBinData_Create();
success := CkBinData_LoadFile(bdRequestBody,'your-file-path');
if (success <> True) then
begin
Memo1.Lines.Add('Failed to load your-file-path');
Exit;
end;
// Adds the "Authorization: Bearer your-auth-token" header.
CkHttp_putAuthToken(http,'your-auth-token');
resp := CkHttpResponse_Create();
success := CkHttp_HttpBd(http,'POST','https://your-webapp-hostname:your-webapp-port/v1/media',bdRequestBody,'image/jpeg',resp);
if (success = False) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
sbResponseBody := CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);
Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));
respStatusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
begin
Memo1.Lines.Add('Response Header:');
Memo1.Lines.Add(CkHttpResponse__header(resp));
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 := CkJsonObject_SizeOfArray(jResp,'messages');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
id := CkJsonObject__stringOf(jResp,'messages[i].id');
i := i + 1;
end;
CkHttp_Dispose(http);
CkBinData_Dispose(bdRequestBody);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);