Sample code for 30+ languages & platforms
DataFlex

Box.com Upload File

See more Box Examples

Demonstrates how to upload a file to box.com.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Variant vOauth2
    Handle hoOauth2
    Boolean iBAutoReconnect
    Handle hoJsonAttr
    Variant vFileDataObj
    Handle hoFileDataObj
    String sResponseBody
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // ------------------------------------------------------------------------------------------
    // Important:
    // See this information about Box.com Service Accounts
    // Box.com will automatically generate a Service Account where the name of the account is the name of your App.� 
    // When you make API calls, it is for this service account, and the files that exist and what you see are not the same as your normal account.
    // ------------------------------------------------------------------------------------------

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    //   Provide a previously obtained OAuth2 access token.
    Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2
    If (Not(IsComObjectCreated(hoOauth2))) Begin
        Send CreateComObject of hoOauth2
    End
    Set ComAccessToken Of hoOauth2 To "BOX_ACCESS_TOKEN"
    Get pvComObject of hoOauth2 to vOauth2
    Get ComSetAuthOAuth2 Of hoRest vOauth2 To iSuccess

    // First, make the initial connection.
    // A single REST object, once connected, can be used for many Box REST API calls.
    // The auto-reconnect indicates that if the already-established HTTPS connection is closed,
    // then it will be automatically re-established as needed.
    Move True To iBAutoReconnect

    // ----------------------------------------------------------------------
    // IMPORTANT: Note that the domain is "upload.box.com", not "api.box.com"
    // ----------------------------------------------------------------------
    Get ComConnect Of hoRest "upload.box.com" 443 True iBAutoReconnect To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // The request body uses the "multipart/form-data" format to transmit two "parts". 
    // The first part is called "attributes" and contains a JSON object with information about the file, including the name of the file 
    // and the ID of the parent folder. The second part contains the contents of the file. 
    // (Note that the name of the second "part" is ignored.)

    Get ComAddHeader Of hoRest "Content-Type" "multipart/form-data" To iSuccess

    // Provide the content for each part of the request...

    // First the JSON attributes.  Use "0" for the root folder.
    //   {"name":"penguins.jpg", "parent":{"id":"0"}}
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonAttr
    If (Not(IsComObjectCreated(hoJsonAttr))) Begin
        Send CreateComObject of hoJsonAttr
    End
    Get ComUpdateString Of hoJsonAttr "name" "penguins.jpg" To iSuccess
    Get ComUpdateString Of hoJsonAttr "parent.id" "0" To iSuccess

    Set ComPartSelector Of hoRest To "1"
    Get ComAddHeader Of hoRest "Content-Disposition" 'form-data; name="attributes"; ' To iSuccess
    Get ComEmit Of hoJsonAttr To sTemp1
    Get ComSetMultipartBodyString Of hoRest sTemp1 To iSuccess

    Set ComPartSelector Of hoRest To "2"
    Get ComAddHeader Of hoRest "Content-Disposition" 'form-data; name="file"; filename="penguins.jpg"' To iSuccess
    // "application/octet-stream" can be safely used for any type file..
    Get ComAddHeader Of hoRest "Content-Type" "application/octet-stream" To iSuccess

    // Load the file into a binary data object, and then upload..
    Get Create (RefClass(cComChilkatBinData)) To hoFileDataObj
    If (Not(IsComObjectCreated(hoFileDataObj))) Begin
        Send CreateComObject of hoFileDataObj
    End
    Get ComLoadFile Of hoFileDataObj "qa_data/jpg/penguins.jpg" To iSuccess
    Get pvComObject of hoFileDataObj to vFileDataObj
    Get ComSetMultipartBodyBd Of hoRest vFileDataObj To iSuccess

    // Restore the PartSelector to "0" (for safety, in case something else sends another request on this object)
    Set ComPartSelector Of hoRest To "0"

    // Send the multipart/form-data request, which uploads the file contained in fileDataObj
    Get ComFullRequestMultipart Of hoRest "POST" "/api/2.0/files/content" To sResponseBody
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // A 201 is received for a successful upload
    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 201) Begin
        Showln "Box.com upload failed."
        Showln "Request header:"
        Get ComLastRequestHeader Of hoRest To sTemp1
        Showln sTemp1
        Showln "---"
        Get ComResponseStatusCode Of hoRest To iTemp1
        Showln "Response status code = " iTemp1
        Showln "Response body:"
        Showln sResponseBody
        Procedure_Return
    End

    Showln "File uploaded."


End_Procedure