Sample code for 30+ languages & platforms
Tcl

Send a REST Request using StringBuilder Bodies

See more REST Examples

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

Background. Using StringBuilder objects for the request and response bodies avoids extra string conversions, which is helpful when working with large payloads.

Chilkat Tcl Downloads

Tcl

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
}

#  FullRequestSb sends a request whose text body is read from a StringBuilder and stores the text
#  response body in a second StringBuilder.  This avoids extra string conversions for large bodies.
set sbRequest [new_CkStringBuilder]

CkStringBuilder_Append $sbRequest "{ \"query\": \"chilkat\" }"

CkRest_AddHeader $rest "Content-Type" "application/json"

set sbResponse [new_CkStringBuilder]

set success [CkRest_FullRequestSb $rest "POST" "/api/search" $sbRequest $sbResponse]
if {$success == 0} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkStringBuilder $sbRequest
    delete_CkStringBuilder $sbResponse
    exit
}

set responseText [CkStringBuilder_getAsString $sbResponse]
if {[CkStringBuilder_get_LastMethodSuccess $sbResponse] == 0} then {
    puts [CkStringBuilder_lastErrorText $sbResponse]
    delete_CkRest $rest
    delete_CkStringBuilder $sbRequest
    delete_CkStringBuilder $sbResponse
    exit
}

puts "$responseText"

delete_CkRest $rest
delete_CkStringBuilder $sbRequest
delete_CkStringBuilder $sbResponse