Sample code for 30+ languages & platforms
DataFlex

Set a Multipart Part Body from a Stream

See more REST Examples

Demonstrates Rest.SetMultipartBodyStream, which uses a Stream as the body source for the selected multipart part.

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. Streaming a part body is appropriate for large parts because the data is read incrementally from the source rather than being buffered entirely in memory. The part is otherwise assembled the same way — select the part, set its headers, then set its body.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Boolean iBAutoReconnect
    Variant vPartStream
    Handle hoPartStream
    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" "application/octet-stream" To iSuccess

    //  Use a Stream as the body source for the selected part.  The stream's source is a local file,
    //  which is appropriate for large parts.
    Get Create (RefClass(cComChilkatStream)) To hoPartStream
    If (Not(IsComObjectCreated(hoPartStream))) Begin
        Send CreateComObject of hoPartStream
    End
    Set ComSourceFile Of hoPartStream To "qa_data/attachment.bin"
    Get pvComObject of hoPartStream to vPartStream
    Get ComSetMultipartBodyStream Of hoRest vPartStream 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