Sample code for 30+ languages & platforms
DataFlex 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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCurl
    Handle hoSetupCommand
    Integer iSetupReplacements
    String sFolderName
    String sFileName
    Handle hoQueryText
    Variant vSetupPlan
    Handle hoSetupPlan
    Handle hoAuthorizedCommand1
    Integer iHeaderReplacement1
    String sTemp1

    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.
    //  Examine the plan without running any HTTP requests.
    Get Create (RefClass(cComChilkatJsonObject)) To hoSetupPlan
    If (Not(IsComObjectCreated(hoSetupPlan))) Begin
        Send CreateComObject of hoSetupPlan
    End
    Set ComEmitCompact Of hoSetupPlan 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 hoSetupPlan to vSetupPlan
    Get ComExaminePlan Of hoCurl sTemp1 vSetupPlan To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComEmit Of hoSetupPlan To sTemp1
    Showln sTemp1
    Showln "Keep this HttpCurl object for the search-based download operation."


End_Procedure