Sample code for 30+ languages & platforms
Tcl

Read the REST Response Body into a StringBuilder

See more REST Examples

Demonstrates Rest.ReadRespSb, which reads the response body as text into a StringBuilder, replacing its previous contents.

Background. The staged request model separates sending from reading: a SendReq method transmits the request, ReadResponseHeader reads the status line and headers, and a ReadResp method reads the body. This gives fine control over large or streamed messages compared with the single-call full-request methods.

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
}

set success [CkRest_SendReqNoBody $rest "GET" "/api/items/123"]
if {$success == 0} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    exit
}

set statusCode [CkRest_ReadResponseHeader $rest]
if {$statusCode < 0} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    exit
}

#  Read the response body as text into a StringBuilder, replacing its previous contents.
set sbResponse [new_CkStringBuilder]

set success [CkRest_ReadRespSb $rest $sbResponse]
if {$success == 0} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkStringBuilder $sbResponse
    exit
}

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

puts "$responseBody"

delete_CkRest $rest
delete_CkStringBuilder $sbResponse