Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.7.0+

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Curl
oleobject loo_SetupCommand
long li_SetupReplacements
string ls_FolderName
string ls_FileName
oleobject loo_QueryText
oleobject loo_SetupPlan
oleobject loo_AuthorizedCommand1
long li_HeaderReplacement1

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.
//  Examine the plan without running any HTTP requests.
loo_SetupPlan = create oleobject
li_rc = loo_SetupPlan.ConnectToNewObject("Chilkat.JsonObject")

loo_SetupPlan.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_SetupPlan)
if li_Success <> 1 then
    Write-Debug loo_Curl.LastErrorText
    destroy loo_Curl
    destroy loo_SetupCommand
    destroy loo_QueryText
    destroy loo_SetupPlan
    destroy loo_AuthorizedCommand1
    return
end if

Write-Debug loo_SetupPlan.Emit()
Write-Debug "Keep this HttpCurl object for the search-based download operation."


destroy loo_Curl
destroy loo_SetupCommand
destroy loo_QueryText
destroy loo_SetupPlan
destroy loo_AuthorizedCommand1