Delphi DLL
Delphi DLL
Capture the Composed REST Request for Debugging
See more REST Examples
Demonstrates Rest.GetLastDebugRequest, which copies the fully composed HTTP request (request line, headers, and body) into a BinData. The example builds a two-part multipart request so the captured output shows the multipart boundaries, part headers, and part bodies. This works when the DebugMode property was enabled while the request was composed.
Background. When DebugMode is enabled, Chilkat builds the request but does not transmit it, so GetLastDebugRequest can reveal the exact bytes that would be sent. Inspecting the composed request is valuable for diagnosing API problems.
Chilkat Delphi DLL Downloads
var
success: Boolean;
rest: HCkRest;
bTls: Boolean;
bAutoReconnect: Boolean;
bdRequest: HCkBinData;
requestText: 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;
// Enable debug mode so the fully composed HTTP request is captured instead of being sent.
CkRest_putDebugMode(rest,True);
// Build a multipart request with two parts. The captured debug output will show the multipart
// boundaries, part headers, and part bodies.
CkRest_putPartSelector(rest,'1');
CkRest_AddHeader(rest,'Content-Type','text/plain');
CkRest_AddHeader(rest,'Content-Disposition','form-data; name="field1"');
success := CkRest_SetMultipartBodyString(rest,'value1');
if (success = False) then
begin
Memo1.Lines.Add(CkRest__lastErrorText(rest));
Exit;
end;
CkRest_putPartSelector(rest,'2');
CkRest_AddHeader(rest,'Content-Type','application/json');
CkRest_AddHeader(rest,'Content-Disposition','form-data; name="metadata"');
success := CkRest_SetMultipartBodyString(rest,'{ "active": true }');
if (success = False) then
begin
Memo1.Lines.Add(CkRest__lastErrorText(rest));
Exit;
end;
// Compose the multipart request. In debug mode the request is built but not transmitted.
success := CkRest_SendReqMultipart(rest,'POST','/api/upload');
if (success = False) then
begin
Memo1.Lines.Add(CkRest__lastErrorText(rest));
Exit;
end;
// Copy the composed request (request line, headers, and multipart body that would be sent) into a
// BinData, then view it as text.
bdRequest := CkBinData_Create();
CkRest_GetLastDebugRequest(rest,bdRequest);
requestText := CkBinData__getString(bdRequest,'utf-8');
if (CkBinData_getLastMethodSuccess(bdRequest) = False) then
begin
Memo1.Lines.Add(CkBinData__lastErrorText(bdRequest));
Exit;
end;
Memo1.Lines.Add(requestText);
CkRest_Dispose(rest);
CkBinData_Dispose(bdRequest);