Sample code for 30+ languages & platforms
DataFlex

Send a REST Request with a Streamed Body

See more REST Examples

Demonstrates Rest.FullRequestStream, which sends a complete request whose body is read from a Stream, then returns the response body as text.

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 the body is appropriate for large uploads because the data is read incrementally from the source (here a file) rather than being held entirely in memory.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Boolean iBAutoReconnect
    Variant vBodyStream
    Handle hoBodyStream
    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.

    //  The request body is read from a Stream.  Here the stream's source is a local file, which is
    //  appropriate for large uploads because the data need not be held entirely in memory.
    Get Create (RefClass(cComChilkatStream)) To hoBodyStream
    If (Not(IsComObjectCreated(hoBodyStream))) Begin
        Send CreateComObject of hoBodyStream
    End
    Set ComSourceFile Of hoBodyStream To "qa_data/upload.json"

    Get ComAddHeader Of hoRest "Content-Type" "application/json" To iSuccess

    //  The 3rd argument is the Stream that supplies the request body.
    Get pvComObject of hoBodyStream to vBodyStream
    Get ComFullRequestStream Of hoRest "POST" "/api/upload" vBodyStream 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