DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoCurl
Handle hoSetupCommand
Integer iSetupReplacements
String sFolderName
String sFileName
Handle hoQueryText
String sLocalPath
Variant vPlan
Handle hoPlan
Handle hoAuthorizedCommand1
Integer iHeaderReplacement1
Handle hoAuthorizedCommand2
Integer iHeaderReplacement2
Variant vContents
Handle hoContents
String sTemp1
String sTemp2
String sTemp3
String sTemp4
String sTemp5
Integer iTemp1
Move False To iSuccess
// Requires Chilkat 11.7.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatHttpCurl)) To hoCurl
If (Not(IsComObjectCreated(hoCurl))) Begin
Send CreateComObject of hoCurl
End
Send ComSetVar To hoCurl "access_token" "BOX_ACCESS_TOKEN"
Get Create (RefClass(cComChilkatStringBuilder)) To hoSetupCommand
If (Not(IsComObjectCreated(hoSetupCommand))) Begin
Send CreateComObject of hoSetupCommand
End
// searchFolder: Box searches names; quotes request a phrase match.
Get ComSetString Of hoSetupCommand '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"' To iSuccess
Get ComGetVar Of hoCurl "access_token" To sTemp1
Get ComReplace Of hoSetupCommand "ACCESS_TOKEN" sTemp1 To iSetupReplacements
Get ComGetAsString Of hoSetupCommand To sTemp1
Get ComAddFunction Of hoCurl "searchFolder" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
// Deferred selection: folder_name need not be set during setup.
Get ComAddOutput2 Of hoCurl "searchFolder" "entries" "name" "{{folder_name}}" True "id" "folder_id" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComAddOutput2 Of hoCurl "searchFolder" "entries" "name" "{{folder_name}}" True "name" "folder_result_name" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComAddOutput2 Of hoCurl "searchFolder" "entries" "name" "{{folder_name}}" True "parent.id" "folder_parent_id" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComAddOutput Of hoCurl "searchFolder" "total_count" "folder_search_count" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
// searchFile: Box searches names; quotes request a phrase match.
Get ComSetString Of hoSetupCommand '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"' To iSuccess
Get ComGetVar Of hoCurl "access_token" To sTemp1
Get ComReplace Of hoSetupCommand "ACCESS_TOKEN" sTemp1 To iSetupReplacements
Get ComGetAsString Of hoSetupCommand To sTemp1
Get ComAddFunction Of hoCurl "searchFile" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
// Deferred selection: file_name need not be set during setup.
Get ComAddOutput2 Of hoCurl "searchFile" "entries" "name" "{{file_name}}" True "id" "file_id" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComAddOutput2 Of hoCurl "searchFile" "entries" "name" "{{file_name}}" True "name" "file_result_name" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComAddOutput2 Of hoCurl "searchFile" "entries" "name" "{{file_name}}" True "parent.id" "file_parent_id" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComAddOutput Of hoCurl "searchFile" "total_count" "file_search_count" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Move "images" To sFolderName
Move "penguins.jpg" To sFileName
// Encode the quoted search queries separately from the literal output filters.
// In this build, URL variable substitution does not encode '&' in a name.
Get Create (RefClass(cComChilkatStringBuilder)) To hoQueryText
If (Not(IsComObjectCreated(hoQueryText))) Begin
Send CreateComObject of hoQueryText
End
Get ComSetString Of hoQueryText '"' To iSuccess
Get ComAppend Of hoQueryText sFolderName To iSuccess
Get ComAppend Of hoQueryText '"' To iSuccess
Get ComGetEncoded Of hoQueryText "url" "utf-8" To sTemp1
Send ComSetVar To hoCurl "folder_query" sTemp1
Get ComSetString Of hoQueryText '"' To iSuccess
Get ComAppend Of hoQueryText sFileName To iSuccess
Get ComAppend Of hoQueryText '"' To iSuccess
Get ComGetEncoded Of hoQueryText "url" "utf-8" To sTemp1
Send ComSetVar To hoCurl "file_query" sTemp1
Get ComGetVar Of hoCurl "folder_name" To sTemp1
If (sTemp1 <> sFolderName) Begin
Send ComClearVar To hoCurl "folder_id"
Send ComClearVar To hoCurl "folder_result_name"
Send ComClearVar To hoCurl "folder_parent_id"
Send ComClearVar To hoCurl "folder_search_count"
Send ComClearVar To hoCurl "file_id"
Send ComClearVar To hoCurl "file_result_name"
Send ComClearVar To hoCurl "file_parent_id"
Send ComClearVar To hoCurl "file_search_count"
Send ComSetVar To hoCurl "folder_name" sFolderName
End
Get ComGetVar Of hoCurl "file_name" To sTemp1
If (sTemp1 <> sFileName) Begin
Send ComClearVar To hoCurl "file_id"
Send ComClearVar To hoCurl "file_result_name"
Send ComClearVar To hoCurl "file_parent_id"
Send ComClearVar To hoCurl "file_search_count"
Send ComSetVar To hoCurl "file_name" sFileName
End
// With unchanged names, cached IDs and validation fields can be reused.
// Clear the corresponding cached fields to refresh after a remote rename/move.
Move "qa_output/penguins.jpg" To sLocalPath
// A cold plan is searchFolder -> searchFile -> targetCurl.
// No listing endpoint or manual search call is used.
Get Create (RefClass(cComChilkatJsonObject)) To hoPlan
If (Not(IsComObjectCreated(hoPlan))) Begin
Send CreateComObject of hoPlan
End
Set ComEmitCompact Of hoPlan To False
Get Create (RefClass(cComChilkatStringBuilder)) To hoAuthorizedCommand1
If (Not(IsComObjectCreated(hoAuthorizedCommand1))) Begin
Send CreateComObject of hoAuthorizedCommand1
End
Get ComSetString Of hoAuthorizedCommand1 'curl -X GET "https://api.box.com/2.0/files/{{file_id}}/content" -H "Authorization: Bearer {{access_token}}" -L' To iSuccess
Get ComGetVar Of hoCurl "access_token" To sTemp1
Get ComReplace Of hoAuthorizedCommand1 "{{access_token}}" sTemp1 To iHeaderReplacement1
Get ComGetAsString Of hoAuthorizedCommand1 To sTemp1
Get pvComObject of hoPlan to vPlan
Get ComExaminePlan Of hoCurl sTemp1 vPlan To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComEmit Of hoPlan To sTemp1
Showln sTemp1
// Keep the candidate download in memory; do not write an unvalidated result.
Set ComResponseFilePath Of hoCurl To ""
Send ComClearTargetOutput To hoCurl "*"
Get Create (RefClass(cComChilkatStringBuilder)) To hoAuthorizedCommand2
If (Not(IsComObjectCreated(hoAuthorizedCommand2))) Begin
Send CreateComObject of hoAuthorizedCommand2
End
Get ComSetString Of hoAuthorizedCommand2 'curl -X GET "https://api.box.com/2.0/files/{{file_id}}/content" -H "Authorization: Bearer {{access_token}}" -L' To iSuccess
Get ComGetVar Of hoCurl "access_token" To sTemp1
Get ComReplace Of hoAuthorizedCommand2 "{{access_token}}" sTemp1 To iHeaderReplacement2
Get ComGetAsString Of hoAuthorizedCommand2 To sTemp1
Get ComDoYourThing Of hoCurl sTemp1 To iSuccess
Get ComStatusCode Of hoCurl To iTemp1
If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
Get ComStatusCode Of hoCurl To iTemp1
Showln "Box request failed. HTTP status: " iTemp1
Get ComFailReason Of hoCurl To iTemp1
Showln "Failure reason: " iTemp1
Procedure_Return
End
// AddOutput2 supplies candidate IDs. Box's phrase search and recursive
// ancestor filter are not an exact path lookup, so validate before saving.
Get ComGetVar Of hoCurl "folder_search_count" To sTemp1
Get ComGetVar Of hoCurl "file_search_count" To sTemp2
If ((sTemp1 <> "1") Or (sTemp2 <> "1")) Begin
Showln "Search was ambiguous or incomplete. Expected exactly one result per search; no file was saved."
Get ComGetVar Of hoCurl "folder_search_count" To sTemp1
Get ComGetVar Of hoCurl "file_search_count" To sTemp2
Showln "Folder results: " sTemp1 " File results: " sTemp2
Send ComClearVar To hoCurl "folder_id"
Send ComClearVar To hoCurl "folder_result_name"
Send ComClearVar To hoCurl "folder_parent_id"
Send ComClearVar To hoCurl "folder_search_count"
Send ComClearVar To hoCurl "file_id"
Send ComClearVar To hoCurl "file_result_name"
Send ComClearVar To hoCurl "file_parent_id"
Send ComClearVar To hoCurl "file_search_count"
Procedure_Return
End
Get ComGetVar Of hoCurl "folder_result_name" To sTemp1
Get ComGetVar Of hoCurl "folder_parent_id" To sTemp2
Get ComGetVar Of hoCurl "file_result_name" To sTemp3
Get ComGetVar Of hoCurl "file_parent_id" To sTemp4
Get ComGetVar Of hoCurl "folder_id" To sTemp5
If ((sTemp1 <> sFolderName) Or (sTemp2 <> "0") Or (sTemp3 <> sFileName) Or (sTemp4 <> sTemp5)) Begin
Showln "The search result is not the exact root-folder/filename requested; no file was saved."
Send ComClearVar To hoCurl "folder_id"
Send ComClearVar To hoCurl "folder_result_name"
Send ComClearVar To hoCurl "folder_parent_id"
Send ComClearVar To hoCurl "folder_search_count"
Send ComClearVar To hoCurl "file_id"
Send ComClearVar To hoCurl "file_result_name"
Send ComClearVar To hoCurl "file_parent_id"
Send ComClearVar To hoCurl "file_search_count"
Procedure_Return
End
// 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.
Get Create (RefClass(cComChilkatBinData)) To hoContents
If (Not(IsComObjectCreated(hoContents))) Begin
Send CreateComObject of hoContents
End
Get pvComObject of hoContents to vContents
Get ComGetResponseBd Of hoCurl vContents To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCurl To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComWriteFile Of hoContents sLocalPath To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoContents To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Downloaded to " sLocalPath
End_Procedure