Sample code for 30+ languages & platforms
Tcl

HttpPostJson2Async Example

Demonstrates use of the HttpPostJson2Async method.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code

# See PostJson2 Example for the synchronous equivalent of this example.

set http [new_CkHttp]

# Sends a POST equivalent to the following CURL command:

# curl -X POST https://sandbox.plaid.com/institutions/get \
#   -H 'content-type: application/json' \
#   -d '{
#     "client_id": String,
#     "secret":String,
#     "count": Number,
#     "offset": Number
#   }'

# Suppress some default headers that would automatically added..
CkHttp_put_AcceptCharset $http ""
CkHttp_put_UserAgent $http ""
CkHttp_put_AcceptLanguage $http ""
CkHttp_put_AllowGzip $http 0

set jsonBody "{\"client_id\": \"PLAID_CLIENT_ID\", \"secret\":\"PLAID_SECRET\", \"count\": 10, \"offset\": 0}"
# task is a CkTask
set task [CkHttp_PostJson2Async $http "https://sandbox.plaid.com/institutions/get" "application/json" $jsonBody]
if {[CkHttp_get_LastMethodSuccess $http] == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    exit
}

# Start the background task.
set success [CkTask_Run $task]
if {!$success} then {
    puts [CkTask_lastErrorText $task]
    delete_CkTask $task

    delete_CkHttp $http
    exit
}

# The application is now free to do anything else
# while the HTTP POST is in progress

# For this example, we'll simply sleep and periodically
# check to see if the HTTP POST is finished.  
while {[CkTask_get_Finished $task] != 1} {
    # Sleep 100 ms.
    CkTask_SleepMs $task 100
}

# When we get here, the task is either finished successfully or not, or was canceled/aborted.

# If the task was "canceled", it was canceled prior to actually starting.  This could
# happen if the task was canceled while waiting in a thread pool queue to be scheduled by Chilkat's
# background thread pool scheduler.  

# If the task was "aborted", it indicates that it was canceled while running in a background thread.  
# The ResultErrorText will likely indicate that the task was aborted.

# If the task "completed", then it ran to completion, but the actual success/failure of the method
# is determined by getting the result of the underlying method call.
# A task status of 7 indicates completion.
if {[CkTask_get_StatusInt $task] != 7} then {
    puts "Task did not complete."
    puts "task status: [CkTask_status $task]"
    delete_CkTask $task

    delete_CkHttp $http
    exit
}

# When called synchronously, the PostJson2 method returns an HTTP response object,
# unless the method failed and there was no response to be had.
# Check to see if PostJson2 method failed (and thus there is no response object).
if {[CkTask_get_TaskSuccess $task] == 0} then {
    puts "The underlying task failed, and there is no HTTP response object."
    puts "The LastErrorText for the underlying method call is:"
    puts [CkTask_resultErrorText $task]
    delete_CkTask $task

    delete_CkHttp $http
    exit
}

# To get the HTTP response object, create a new instance and load it with the result from the task.
set resp [new_CkHttpResponse]

CkHttpResponse_LoadTaskResult $resp $task
delete_CkTask $task

set statusCode [CkHttpResponse_get_StatusCode $resp]
puts "Response status code = $statusCode"

# Examine the JSON response..
set json [new_CkJsonObject]

CkJsonObject_Load $json [CkHttpResponse_bodyStr $resp]
CkJsonObject_put_EmitCompact $json 0
puts "JSON Response Body:"
puts [CkJsonObject_emit $json]

delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $json