Sample code for 30+ languages & platforms
Tcl

GetHarvest - Get Client Contacts

See more GetHarvest Examples

Returns a list of your 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 "https://api.harvestapp.com/v2/contacts" \
#   -H "Authorization: Bearer ACCESS_TOKEN" \
#   -H "Harvest-Account-Id: ACCOUNT_ID" \
#   -H "User-Agent: MyApp (yourname@example.com)"

CkHttp_SetRequestHeader $http "User-Agent" "MyApp (yourname@example.com)"
CkHttp_SetRequestHeader $http "Authorization" "Bearer ACCESS_TOKEN"
CkHttp_SetRequestHeader $http "Harvest-Account-Id" "ACCOUNT_ID"

set sbResponseBody [new_CkStringBuilder]

set success [CkHttp_QuickGetSb $http "https://api.harvestapp.com/v2/contacts" $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:

# {
#   "contacts": [
#     {
#       "id": 4706479,
#       "title": "Owner",
#       "first_name": "Jane",
#       "last_name": "Doe",
#       "email": "janedoe@example.com",
#       "phone_office": "(203) 697-8885",
#       "phone_mobile": "(203) 697-8886",
#       "fax": "(203) 697-8887",
#       "created_at": "2017-06-26T21:20:07Z",
#       "updated_at": "2017-06-26T21:27:07Z",
#       "client": {
#         "id": 5735774,
#         "name": "ABC Corp"
#       }
#     },
#     {
#       "id": 4706453,
#       "title": "Manager",
#       "first_name": "Richard",
#       "last_name": "Roe",
#       "email": "richardroe@example.com",
#       "phone_office": "(318) 515-5905",
#       "phone_mobile": "(318) 515-5906",
#       "fax": "(318) 515-5907",
#       "created_at": "2017-06-26T21:06:55Z",
#       "updated_at": "2017-06-26T21:27:20Z",
#       "client": {
#         "id": 5735776,
#         "name": "123 Industries"
#       }
#     }
#   ],
#   "per_page": 100,
#   "total_pages": 1,
#   "total_entries": 2,
#   "next_page": null,
#   "previous_page": null,
#   "page": 1,
#   "links": {
#     "first": "https://api.harvestapp.com/v2/contacts?page=1&per_page=100",
#     "next": null,
#     "previous": null,
#     "last": "https://api.harvestapp.com/v2/contacts?page=1&per_page=100"
#   }
# }

# 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 per_page [CkJsonObject_IntOf $jResp "per_page"]
set total_pages [CkJsonObject_IntOf $jResp "total_pages"]
set total_entries [CkJsonObject_IntOf $jResp "total_entries"]
set next_page [CkJsonObject_stringOf $jResp "next_page"]
set previous_page [CkJsonObject_stringOf $jResp "previous_page"]
set page [CkJsonObject_IntOf $jResp "page"]
set linksFirst [CkJsonObject_stringOf $jResp "links.first"]
set linksNext [CkJsonObject_stringOf $jResp "links.next"]
set linksPrevious [CkJsonObject_stringOf $jResp "links.previous"]
set linksLast [CkJsonObject_stringOf $jResp "links.last"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "contacts"]
while {$i < $count_i} {
    CkJsonObject_put_I $jResp $i
    set id [CkJsonObject_IntOf $jResp "contacts[i].id"]
    set title [CkJsonObject_stringOf $jResp "contacts[i].title"]
    set first_name [CkJsonObject_stringOf $jResp "contacts[i].first_name"]
    set last_name [CkJsonObject_stringOf $jResp "contacts[i].last_name"]
    set email [CkJsonObject_stringOf $jResp "contacts[i].email"]
    set phone_office [CkJsonObject_stringOf $jResp "contacts[i].phone_office"]
    set phone_mobile [CkJsonObject_stringOf $jResp "contacts[i].phone_mobile"]
    set fax [CkJsonObject_stringOf $jResp "contacts[i].fax"]
    set created_at [CkJsonObject_stringOf $jResp "contacts[i].created_at"]
    set updated_at [CkJsonObject_stringOf $jResp "contacts[i].updated_at"]
    set clientId [CkJsonObject_IntOf $jResp "contacts[i].client.id"]
    set clientName [CkJsonObject_stringOf $jResp "contacts[i].client.name"]
    set i [expr $i + 1]
}

delete_CkHttp $http
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp