C
C
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 C Downloads
#include <C_CkRest.h>
#include <C_CkBinData.h>
void ChilkatSample(void)
{
BOOL success;
HCkRest rest;
BOOL bTls;
BOOL bAutoReconnect;
HCkBinData bdRequest;
const char *requestText;
success = FALSE;
rest = CkRest_Create();
bTls = TRUE;
bAutoReconnect = TRUE;
success = CkRest_Connect(rest,"example.com",443,bTls,bAutoReconnect);
if (success == FALSE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
return;
}
// 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) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
return;
}
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) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
return;
}
// Compose the multipart request. In debug mode the request is built but not transmitted.
success = CkRest_SendReqMultipart(rest,"POST","/api/upload");
if (success == FALSE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
return;
}
// 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) {
printf("%s\n",CkBinData_lastErrorText(bdRequest));
CkRest_Dispose(rest);
CkBinData_Dispose(bdRequest);
return;
}
printf("%s\n",requestText);
CkRest_Dispose(rest);
CkBinData_Dispose(bdRequest);
}