Sample code for 30+ languages & platforms
DataFlex

Set a Multipart Part Body from BinData

See more REST Examples

Demonstrates Rest.SetMultipartBodyBd, which sets the binary body of the selected multipart part from a BinData.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. A multipart request is assembled part by part: the part selector chooses the part being built, header fields describe it, and a SetMultipartBody method supplies its content. The request is then sent with a multipart send or full-request method.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Boolean iBAutoReconnect
    Variant vBdBody
    Handle hoBdBody
    Boolean iBLoaded
    String sResponseText
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End
    Move True To iBTls
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest "example.com" 443 iBTls iBAutoReconnect To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  The file paths are relative to the application's current working directory.  Absolute paths
    //  may also be used.  Supply the paths appropriate to your own environment.

    //  Select the multipart part to build, and set its header fields.
    Set ComPartSelector Of hoRest To "1"
    Get ComAddHeader Of hoRest "Content-Disposition" 'form-data; name="field1"' To iSuccess
    Get ComAddHeader Of hoRest "Content-Type" "image/png" To iSuccess

    //  Set the binary body of the selected part from a BinData.
    Get Create (RefClass(cComChilkatBinData)) To hoBdBody
    If (Not(IsComObjectCreated(hoBdBody))) Begin
        Send CreateComObject of hoBdBody
    End
    Get ComLoadFile Of hoBdBody "qa_data/image.png" To iBLoaded
    If (iBLoaded <> True) Begin
        Showln "Failed to load the part body file."
        Procedure_Return
    End

    Get pvComObject of hoBdBody to vBdBody
    Get ComSetMultipartBodyBd Of hoRest vBdBody To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComFullRequestMultipart Of hoRest "POST" "/api/upload" To sResponseText
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sResponseText


End_Procedure