Tcl
Tcl
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 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
}
# 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.
set bdRequest [new_CkBinData]
set bLoaded [CkBinData_LoadFile $bdRequest "qa_data/image.png"]
if {$bLoaded != 1} then {
puts "Failed to load the request body file."
delete_CkRest $rest
delete_CkBinData $bdRequest
exit
}
CkRest_AddHeader $rest "Content-Type" "image/png"
set sbResponse [new_CkStringBuilder]
set success [CkRest_FullRequestBd $rest "PUT" "/api/images/1" $bdRequest $sbResponse]
if {$success == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
delete_CkBinData $bdRequest
delete_CkStringBuilder $sbResponse
exit
}
set responseText [CkStringBuilder_getAsString $sbResponse]
if {[CkStringBuilder_get_LastMethodSuccess $sbResponse] == 0} then {
puts [CkStringBuilder_lastErrorText $sbResponse]
delete_CkRest $rest
delete_CkBinData $bdRequest
delete_CkStringBuilder $sbResponse
exit
}
puts "$responseText"
delete_CkRest $rest
delete_CkBinData $bdRequest
delete_CkStringBuilder $sbResponse