PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
Box: Resolve Folder and File IDs with the HttpCurl Dependency Engine
See more Box Examples
Download images/penguins.jpg using search-based HttpCurl dependencies and one DoYourThing call. Register search requests/output rules once, then supply names. Search results must each contain exactly one item; exact names and immediate parent IDs are checked before saving the downloaded bytes. The candidate content is downloaded into memory before validation, so this example is for small files. Multiple results are rejected instead of paginated or silently choosing a candidate. Search indexing can delay visibility of newly created or renamed items.
See the Box API guide for setup and the complete workflow.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Curl
oleobject loo_SetupCommand
long li_SetupReplacements
string ls_FolderName
string ls_FileName
oleobject loo_QueryText
string ls_LocalPath
oleobject loo_Plan
oleobject loo_AuthorizedCommand1
long li_HeaderReplacement1
oleobject loo_AuthorizedCommand2
long li_HeaderReplacement2
oleobject loo_Contents
li_Success = 0
// Requires Chilkat 11.7.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
li_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.
loo_Curl = create oleobject
li_rc = loo_Curl.ConnectToNewObject("Chilkat.HttpCurl")
if li_rc < 0 then
destroy loo_Curl
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Curl.SetVar("access_token","BOX_ACCESS_TOKEN")
loo_SetupCommand = create oleobject
li_rc = loo_SetupCommand.ConnectToNewObject("Chilkat.StringBuilder")
// searchFolder: Box searches names; quotes request a phrase match.
loo_SetupCommand.SetString("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~"")
li_SetupReplacements = loo_SetupCommand.Replace("ACCESS_TOKEN",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.AddFunction("searchFolder",loo_SetupCommand.GetAsString())
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
return
end if
// Deferred selection: folder_name need not be set during setup.
li_Success = loo_Curl.AddOutput2("searchFolder","entries","name","{{folder_name}}",1,"id","folder_id")
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
return
end if
li_Success = loo_Curl.AddOutput2("searchFolder","entries","name","{{folder_name}}",1,"name","folder_result_name")
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
return
end if
li_Success = loo_Curl.AddOutput2("searchFolder","entries","name","{{folder_name}}",1,"parent.id","folder_parent_id")
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
return
end if
li_Success = loo_Curl.AddOutput("searchFolder","total_count","folder_search_count")
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
return
end if
// searchFile: Box searches names; quotes request a phrase match.
loo_SetupCommand.SetString("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~"")
li_SetupReplacements = loo_SetupCommand.Replace("ACCESS_TOKEN",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.AddFunction("searchFile",loo_SetupCommand.GetAsString())
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
return
end if
// Deferred selection: file_name need not be set during setup.
li_Success = loo_Curl.AddOutput2("searchFile","entries","name","{{file_name}}",1,"id","file_id")
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
return
end if
li_Success = loo_Curl.AddOutput2("searchFile","entries","name","{{file_name}}",1,"name","file_result_name")
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
return
end if
li_Success = loo_Curl.AddOutput2("searchFile","entries","name","{{file_name}}",1,"parent.id","file_parent_id")
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
return
end if
li_Success = loo_Curl.AddOutput("searchFile","total_count","file_search_count")
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
return
end if
// 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.
ls_FolderName = "images"
ls_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.
loo_QueryText = create oleobject
li_rc = loo_QueryText.ConnectToNewObject("Chilkat.StringBuilder")
loo_QueryText.SetString("~"")
loo_QueryText.Append(ls_FolderName)
loo_QueryText.Append("~"")
loo_Curl.SetVar("folder_query",loo_QueryText.GetEncoded("url","utf-8"))
loo_QueryText.SetString("~"")
loo_QueryText.Append(ls_FileName)
loo_QueryText.Append("~"")
loo_Curl.SetVar("file_query",loo_QueryText.GetEncoded("url","utf-8"))
if loo_Curl.GetVar("folder_name") <> ls_FolderName then
loo_Curl.ClearVar("folder_id")
loo_Curl.ClearVar("folder_result_name")
loo_Curl.ClearVar("folder_parent_id")
loo_Curl.ClearVar("folder_search_count")
loo_Curl.ClearVar("file_id")
loo_Curl.ClearVar("file_result_name")
loo_Curl.ClearVar("file_parent_id")
loo_Curl.ClearVar("file_search_count")
loo_Curl.SetVar("folder_name",ls_FolderName)
end if
if loo_Curl.GetVar("file_name") <> ls_FileName then
loo_Curl.ClearVar("file_id")
loo_Curl.ClearVar("file_result_name")
loo_Curl.ClearVar("file_parent_id")
loo_Curl.ClearVar("file_search_count")
loo_Curl.SetVar("file_name",ls_FileName)
end if
// With unchanged names, cached IDs and validation fields can be reused.
// Clear the corresponding cached fields to refresh after a remote rename/move.
ls_LocalPath = "qa_output/penguins.jpg"
// A cold plan is searchFolder -> searchFile -> targetCurl.
// No listing endpoint or manual search call is used.
loo_Plan = create oleobject
li_rc = loo_Plan.ConnectToNewObject("Chilkat.JsonObject")
loo_Plan.EmitCompact = 0
loo_AuthorizedCommand1 = create oleobject
li_rc = loo_AuthorizedCommand1.ConnectToNewObject("Chilkat.StringBuilder")
loo_AuthorizedCommand1.SetString("curl -X GET ~"https://api.box.com/2.0/files/{{file_id}}/content~" -H ~"Authorization: Bearer {{access_token}}~" -L")
li_HeaderReplacement1 = loo_AuthorizedCommand1.Replace("{{access_token}}",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.ExaminePlan(loo_AuthorizedCommand1.GetAsString(),loo_Plan)
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
destroy loo_QueryText
destroy loo_Plan
destroy loo_AuthorizedCommand1
return
end if
Write-Debug loo_Plan.Emit()
// Keep the candidate download in memory; do not write an unvalidated result.
loo_Curl.ResponseFilePath = ""
loo_Curl.ClearTargetOutput("*")
loo_AuthorizedCommand2 = create oleobject
li_rc = loo_AuthorizedCommand2.ConnectToNewObject("Chilkat.StringBuilder")
loo_AuthorizedCommand2.SetString("curl -X GET ~"https://api.box.com/2.0/files/{{file_id}}/content~" -H ~"Authorization: Bearer {{access_token}}~" -L")
li_HeaderReplacement2 = loo_AuthorizedCommand2.Replace("{{access_token}}",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.DoYourThing(loo_AuthorizedCommand2.GetAsString())
if (li_Success <> 1) OR (loo_Curl.StatusCode <> 200) then
Write-Debug "Box request failed. HTTP status: " + string(loo_Curl.StatusCode)
Write-Debug "Failure reason: " + string(loo_Curl.FailReason)
destroy loo_Curl
destroy loo_SetupCommand
destroy loo_QueryText
destroy loo_Plan
destroy loo_AuthorizedCommand1
destroy loo_AuthorizedCommand2
return
end if
// AddOutput2 supplies candidate IDs. Box's phrase search and recursive
// ancestor filter are not an exact path lookup, so validate before saving.
if (loo_Curl.GetVar("folder_search_count") <> "1") OR (loo_Curl.GetVar("file_search_count") <> "1") then
Write-Debug "Search was ambiguous or incomplete. Expected exactly one result per search; no file was saved."
Write-Debug "Folder results: " + loo_Curl.GetVar("folder_search_count") + " File results: " + loo_Curl.GetVar("file_search_count")
loo_Curl.ClearVar("folder_id")
loo_Curl.ClearVar("folder_result_name")
loo_Curl.ClearVar("folder_parent_id")
loo_Curl.ClearVar("folder_search_count")
loo_Curl.ClearVar("file_id")
loo_Curl.ClearVar("file_result_name")
loo_Curl.ClearVar("file_parent_id")
loo_Curl.ClearVar("file_search_count")
destroy loo_Curl
destroy loo_SetupCommand
destroy loo_QueryText
destroy loo_Plan
destroy loo_AuthorizedCommand1
destroy loo_AuthorizedCommand2
return
end if
if (loo_Curl.GetVar("folder_result_name") <> ls_FolderName) OR (loo_Curl.GetVar("folder_parent_id") <> "0") OR (loo_Curl.GetVar("file_result_name") <> ls_FileName) OR (loo_Curl.GetVar("file_parent_id") <> loo_Curl.GetVar("folder_id")) then
Write-Debug "The search result is not the exact root-folder/filename requested; no file was saved."
loo_Curl.ClearVar("folder_id")
loo_Curl.ClearVar("folder_result_name")
loo_Curl.ClearVar("folder_parent_id")
loo_Curl.ClearVar("folder_search_count")
loo_Curl.ClearVar("file_id")
loo_Curl.ClearVar("file_result_name")
loo_Curl.ClearVar("file_parent_id")
loo_Curl.ClearVar("file_search_count")
destroy loo_Curl
destroy loo_SetupCommand
destroy loo_QueryText
destroy loo_Plan
destroy loo_AuthorizedCommand1
destroy loo_AuthorizedCommand2
return
end if
// Validation is after the network download but before writing the destination.
// This in-memory example is intended for small files such as penguins.jpg.
// Create qa_output before running it.
loo_Contents = create oleobject
li_rc = loo_Contents.ConnectToNewObject("Chilkat.BinData")
li_Success = loo_Curl.GetResponseBd(loo_Contents)
if li_Success <> 1 then
Write-Debug loo_Curl.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
destroy loo_QueryText
destroy loo_Plan
destroy loo_AuthorizedCommand1
destroy loo_AuthorizedCommand2
destroy loo_Contents
return
end if
li_Success = loo_Contents.WriteFile(ls_LocalPath)
if li_Success <> 1 then
Write-Debug loo_Contents.LastErrorText
destroy loo_Curl
destroy loo_SetupCommand
destroy loo_QueryText
destroy loo_Plan
destroy loo_AuthorizedCommand1
destroy loo_AuthorizedCommand2
destroy loo_Contents
return
end if
Write-Debug "Downloaded to " + ls_LocalPath
destroy loo_Curl
destroy loo_SetupCommand
destroy loo_QueryText
destroy loo_Plan
destroy loo_AuthorizedCommand1
destroy loo_AuthorizedCommand2
destroy loo_Contents