Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
set rest [new_CkRest]
set bTls 1
set bAutoReconnect 1
set success [CkRest_Connect $rest "example.com" 443 $bTls $bAutoReconnect]
if {$success == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
# Enable debug mode so the fully composed HTTP request is captured instead of being sent.
CkRest_put_DebugMode $rest 1
# Build a multipart request with two parts. The captured debug output will show the multipart
# boundaries, part headers, and part bodies.
CkRest_put_PartSelector $rest "1"
CkRest_AddHeader $rest "Content-Type" "text/plain"
CkRest_AddHeader $rest "Content-Disposition" "form-data; name=\"field1\""
set success [CkRest_SetMultipartBodyString $rest "value1"]
if {$success == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
CkRest_put_PartSelector $rest "2"
CkRest_AddHeader $rest "Content-Type" "application/json"
CkRest_AddHeader $rest "Content-Disposition" "form-data; name=\"metadata\""
set success [CkRest_SetMultipartBodyString $rest "{ \"active\": true }"]
if {$success == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
# Compose the multipart request. In debug mode the request is built but not transmitted.
set success [CkRest_SendReqMultipart $rest "POST" "/api/upload"]
if {$success == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
# Copy the composed request (request line, headers, and multipart body that would be sent) into a
# BinData, then view it as text.
set bdRequest [new_CkBinData]
CkRest_GetLastDebugRequest $rest $bdRequest
set requestText [CkBinData_getString $bdRequest "utf-8"]
if {[CkBinData_get_LastMethodSuccess $bdRequest] == 0} then {
puts [CkBinData_lastErrorText $bdRequest]
delete_CkRest $rest
delete_CkBinData $bdRequest
exit
}
puts "$requestText"
delete_CkRest $rest
delete_CkBinData $bdRequest