Tcl Requires Chilkat v11.7.0+
Tcl
Box: Set Up Reusable HttpCurl Requests and Outputs
See more Box Examples
Register searchFolder and searchFile once, with deferred folder_name and file_name output rules. This review example resolves a folder directly under root and a file directly inside it. The download operation requires exactly one search result at each step and validates exact names and parent IDs. Registration and ExaminePlan send no requests. Other Box examples still have their existing standalone setups.
See the Box API guide for setup and the complete workflow.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Requires Chilkat 11.7.0 or later and a previously unlocked Chilkat bundle.
# See Global Unlock Sample.
# Guide: Box API Guide.
set success 0
# BEGIN ONE-TIME SETUP
# Requires Chilkat 11.7.0. Registration sends no requests.
# Run this once and keep the SAME HttpCurl object for later downloads.
# This focused setup searches for a root folder and a file directly inside it.
set curl [new_CkHttpCurl]
CkHttpCurl_SetVar $curl "access_token" "BOX_ACCESS_TOKEN"
set setupCommand [new_CkStringBuilder]
# searchFolder: Box searches names; quotes request a phrase match.
CkStringBuilder_SetString $setupCommand "curl -X GET \"https://api.box.com/2.0/search?query={{folder_query}}&type=folder&content_types=name&fields=id,type,name,parent&limit=2\" -H \"Authorization: Bearer ACCESS_TOKEN\""
set setupReplacements [CkStringBuilder_Replace $setupCommand "ACCESS_TOKEN" [CkHttpCurl_getVar $curl "access_token"]]
set success [CkHttpCurl_AddFunction $curl "searchFolder" [CkStringBuilder_getAsString $setupCommand]]
if {$success != 1} then {
puts [CkHttpCurl_lastErrorText $curl]
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
exit
}
# Deferred selection: folder_name need not be set during setup.
set success [CkHttpCurl_AddOutput2 $curl "searchFolder" "entries" "name" "{{folder_name}}" 1 "id" "folder_id"]
if {$success != 1} then {
puts [CkHttpCurl_lastErrorText $curl]
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
exit
}
set success [CkHttpCurl_AddOutput2 $curl "searchFolder" "entries" "name" "{{folder_name}}" 1 "name" "folder_result_name"]
if {$success != 1} then {
puts [CkHttpCurl_lastErrorText $curl]
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
exit
}
set success [CkHttpCurl_AddOutput2 $curl "searchFolder" "entries" "name" "{{folder_name}}" 1 "parent.id" "folder_parent_id"]
if {$success != 1} then {
puts [CkHttpCurl_lastErrorText $curl]
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
exit
}
set success [CkHttpCurl_AddOutput $curl "searchFolder" "total_count" "folder_search_count"]
if {$success != 1} then {
puts [CkHttpCurl_lastErrorText $curl]
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
exit
}
# searchFile: Box searches names; quotes request a phrase match.
CkStringBuilder_SetString $setupCommand "curl -X GET \"https://api.box.com/2.0/search?query={{file_query}}&type=file&content_types=name&fields=id,type,name,parent&limit=2&ancestor_folder_ids={{folder_id}}\" -H \"Authorization: Bearer ACCESS_TOKEN\""
set setupReplacements [CkStringBuilder_Replace $setupCommand "ACCESS_TOKEN" [CkHttpCurl_getVar $curl "access_token"]]
set success [CkHttpCurl_AddFunction $curl "searchFile" [CkStringBuilder_getAsString $setupCommand]]
if {$success != 1} then {
puts [CkHttpCurl_lastErrorText $curl]
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
exit
}
# Deferred selection: file_name need not be set during setup.
set success [CkHttpCurl_AddOutput2 $curl "searchFile" "entries" "name" "{{file_name}}" 1 "id" "file_id"]
if {$success != 1} then {
puts [CkHttpCurl_lastErrorText $curl]
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
exit
}
set success [CkHttpCurl_AddOutput2 $curl "searchFile" "entries" "name" "{{file_name}}" 1 "name" "file_result_name"]
if {$success != 1} then {
puts [CkHttpCurl_lastErrorText $curl]
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
exit
}
set success [CkHttpCurl_AddOutput2 $curl "searchFile" "entries" "name" "{{file_name}}" 1 "parent.id" "file_parent_id"]
if {$success != 1} then {
puts [CkHttpCurl_lastErrorText $curl]
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
exit
}
set success [CkHttpCurl_AddOutput $curl "searchFile" "total_count" "file_search_count"]
if {$success != 1} then {
puts [CkHttpCurl_lastErrorText $curl]
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
exit
}
# END ONE-TIME SETUP
# Search is indexed: newly uploaded/renamed items may not appear immediately.
# Search matches phrases, not exact strings, and ancestor filtering includes
# subfolders. The operation validates counts, exact names, and immediate parents.
# limit=2 is enough to demonstrate an ambiguous search; total_count covers all
# results, not just this page. This introductory example requires exactly one
# search result at each step and does not select among multiple candidates.
# BEGIN OPERATION -- use the previously configured object.
# Human-readable inputs. No resource IDs are supplied by the caller.
set folderName "images"
set fileName "penguins.jpg"
# Encode the quoted search queries separately from the literal output filters.
# In this build, URL variable substitution does not encode '&' in a name.
set queryText [new_CkStringBuilder]
CkStringBuilder_SetString $queryText "\""
CkStringBuilder_Append $queryText $folderName
CkStringBuilder_Append $queryText "\""
CkHttpCurl_SetVar $curl "folder_query" [CkStringBuilder_getEncoded $queryText "url" "utf-8"]
CkStringBuilder_SetString $queryText "\""
CkStringBuilder_Append $queryText $fileName
CkStringBuilder_Append $queryText "\""
CkHttpCurl_SetVar $curl "file_query" [CkStringBuilder_getEncoded $queryText "url" "utf-8"]
if {[CkHttpCurl_getVar $curl "folder_name"] != $folderName} then {
CkHttpCurl_ClearVar $curl "folder_id"
CkHttpCurl_ClearVar $curl "folder_result_name"
CkHttpCurl_ClearVar $curl "folder_parent_id"
CkHttpCurl_ClearVar $curl "folder_search_count"
CkHttpCurl_ClearVar $curl "file_id"
CkHttpCurl_ClearVar $curl "file_result_name"
CkHttpCurl_ClearVar $curl "file_parent_id"
CkHttpCurl_ClearVar $curl "file_search_count"
CkHttpCurl_SetVar $curl "folder_name" $folderName
}
if {[CkHttpCurl_getVar $curl "file_name"] != $fileName} then {
CkHttpCurl_ClearVar $curl "file_id"
CkHttpCurl_ClearVar $curl "file_result_name"
CkHttpCurl_ClearVar $curl "file_parent_id"
CkHttpCurl_ClearVar $curl "file_search_count"
CkHttpCurl_SetVar $curl "file_name" $fileName
}
# With unchanged names, cached IDs and validation fields can be reused.
# Clear the corresponding cached fields to refresh after a remote rename/move.
# Examine the plan without running any HTTP requests.
set setupPlan [new_CkJsonObject]
CkJsonObject_put_EmitCompact $setupPlan 0
set authorizedCommand1 [new_CkStringBuilder]
CkStringBuilder_SetString $authorizedCommand1 "curl -X GET \"https://api.box.com/2.0/files/{{file_id}}/content\" -H \"Authorization: Bearer {{access_token}}\" -L"
set headerReplacement1 [CkStringBuilder_Replace $authorizedCommand1 "{{access_token}}" [CkHttpCurl_getVar $curl "access_token"]]
set success [CkHttpCurl_ExaminePlan $curl [CkStringBuilder_getAsString $authorizedCommand1] $setupPlan]
if {$success != 1} then {
puts [CkHttpCurl_lastErrorText $curl]
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
delete_CkStringBuilder $queryText
delete_CkJsonObject $setupPlan
delete_CkStringBuilder $authorizedCommand1
exit
}
puts [CkJsonObject_emit $setupPlan]
puts "Keep this HttpCurl object for the search-based download operation."
delete_CkHttpCurl $curl
delete_CkStringBuilder $setupCommand
delete_CkStringBuilder $queryText
delete_CkJsonObject $setupPlan
delete_CkStringBuilder $authorizedCommand1