Sample code for 30+ languages & platforms
Tcl

SMSAPI - List Contacts

See more SMSAPI Examples

List Contacts

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set http [new_CkHttp]

# Implements the following CURL command:

# curl -X GET -H "Authorization: Bearer token_api_oauth" https://api.smsapi.com/contacts?phone_number=48500000000

# Use the following online tool to generate HTTP code from a CURL command
# Convert a cURL Command to HTTP Source Code

# Adds the "Authorization: Bearer token_api_oauth" header.
CkHttp_put_AuthToken $http "token_api_oauth"

set sbResponseBody [new_CkStringBuilder]

set success [CkHttp_QuickGetSb $http "https://api.smsapi.com/contacts?phone_number=48500000000" $sbResponseBody]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkStringBuilder $sbResponseBody
    exit
}

set jResp [new_CkJsonObject]

CkJsonObject_LoadSb $jResp $sbResponseBody
CkJsonObject_put_EmitCompact $jResp 0

puts "Response Body:"
puts [CkJsonObject_emit $jResp]

set respStatusCode [CkHttp_get_LastStatus $http]
puts "Response Status Code = $respStatusCode"
if {$respStatusCode >= 400} then {
    puts "Response Header:"
    puts [CkHttp_lastHeader $http]
    puts "Failed."
    delete_CkHttp $http
    delete_CkStringBuilder $sbResponseBody
    delete_CkJsonObject $jResp
    exit
}

# Sample JSON response:
# (Sample code for parsing the JSON response is shown below)

# {
#   "size": 1,
#   "collection": [
#     {
#       "id": "5b83ba81a788494a0469490f",
#       "first_name": "name",
#       "last_name": "surname",
#       "phone_number": "48500000000",
#       "email": "bok@smsapi.com",
#       "gender": "male",
#       "city": "City",
#       "country": "Poland",
#       "source": "source",
#       "date_created": "2018-08-27T10:46:57+02:00",
#       "date_updated": "2018-08-27T10:46:57+02:00",
#       "groups": [
#         {
#           "id": "59a3ca1fa78849062837cd0c",
#           "name": "default",
#           "date_created": "2017-08-28T09:45:35+02:00",
#           "date_updated": "2017-08-28T09:45:35+02:00",
#           "description": "",
#           "created_by": "login",
#           "idx": null,
#           "contact_expire_after": null,
#           "contacts_count": null
#         }
#       ]
#     }
#   ]
# }

# Sample code for parsing the JSON response...
# Use the following online tool to generate parsing code from sample JSON:
# Generate Parsing Code from JSON

set date_created [new_CkDtObj]

set date_updated [new_CkDtObj]

set size [CkJsonObject_IntOf $jResp "size"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "collection"]
while {$i < $count_i} {
    CkJsonObject_put_I $jResp $i
    set id [CkJsonObject_stringOf $jResp "collection[i].id"]
    set first_name [CkJsonObject_stringOf $jResp "collection[i].first_name"]
    set last_name [CkJsonObject_stringOf $jResp "collection[i].last_name"]
    set phone_number [CkJsonObject_stringOf $jResp "collection[i].phone_number"]
    set email [CkJsonObject_stringOf $jResp "collection[i].email"]
    set gender [CkJsonObject_stringOf $jResp "collection[i].gender"]
    set city [CkJsonObject_stringOf $jResp "collection[i].city"]
    set country [CkJsonObject_stringOf $jResp "collection[i].country"]
    set source [CkJsonObject_stringOf $jResp "collection[i].source"]
    CkJsonObject_DtOf $jResp "collection[i].date_created" 0 $date_created
    CkJsonObject_DtOf $jResp "collection[i].date_updated" 0 $date_updated
    set j 0
    set count_j [CkJsonObject_SizeOfArray $jResp "collection[i].groups"]
    while {$j < $count_j} {
        CkJsonObject_put_J $jResp $j
        set id [CkJsonObject_stringOf $jResp "collection[i].groups[j].id"]
        set name [CkJsonObject_stringOf $jResp "collection[i].groups[j].name"]
        CkJsonObject_DtOf $jResp "collection[i].groups[j].date_created" 0 $date_created
        CkJsonObject_DtOf $jResp "collection[i].groups[j].date_updated" 0 $date_updated
        set description [CkJsonObject_stringOf $jResp "collection[i].groups[j].description"]
        set created_by [CkJsonObject_stringOf $jResp "collection[i].groups[j].created_by"]
        set idx [CkJsonObject_stringOf $jResp "collection[i].groups[j].idx"]
        set contact_expire_after [CkJsonObject_stringOf $jResp "collection[i].groups[j].contact_expire_after"]
        set contacts_count [CkJsonObject_stringOf $jResp "collection[i].groups[j].contacts_count"]
        set j [expr $j + 1]
    }
    set i [expr $i + 1]
}

delete_CkHttp $http
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp
delete_CkDtObj $date_created
delete_CkDtObj $date_updated