Sample code for 30+ languages & platforms
Delphi DLL

Send a REST Request with a Binary Body

See more REST Examples

Demonstrates Rest.FullRequestBd, which sends a complete request whose binary body is read from a BinData and stores the text response body in a StringBuilder.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. FullRequestBd is used when the request payload is binary, such as an image or other file, while the response is textual (for example a JSON acknowledgement).

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
rest: HCkRest;
bTls: Boolean;
bAutoReconnect: Boolean;
bdRequest: HCkBinData;
bLoaded: Boolean;
sbResponse: HCkStringBuilder;
responseText: PWideChar;

begin
success := False;

rest := CkRest_Create();
bTls := True;
bAutoReconnect := True;
success := CkRest_Connect(rest,'example.com',443,bTls,bAutoReconnect);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

//  The file paths are relative to the application's current working directory.  Absolute paths
//  may also be used.  Supply the paths appropriate to your own environment.

//  FullRequestBd sends a request whose binary body is read from a BinData and stores the text
//  response body in a StringBuilder.
bdRequest := CkBinData_Create();
bLoaded := CkBinData_LoadFile(bdRequest,'qa_data/image.png');
if (bLoaded <> True) then
  begin
    Memo1.Lines.Add('Failed to load the request body file.');
    Exit;
  end;

CkRest_AddHeader(rest,'Content-Type','image/png');

sbResponse := CkStringBuilder_Create();
success := CkRest_FullRequestBd(rest,'PUT','/api/images/1',bdRequest,sbResponse);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

responseText := CkStringBuilder__getAsString(sbResponse);
if (CkStringBuilder_getLastMethodSuccess(sbResponse) = False) then
  begin
    Memo1.Lines.Add(CkStringBuilder__lastErrorText(sbResponse));
    Exit;
  end;
Memo1.Lines.Add(responseText);

CkRest_Dispose(rest);
CkBinData_Dispose(bdRequest);
CkStringBuilder_Dispose(sbResponse);