Sample code for 30+ languages & platforms
DataFlex

Send a REST Request with a Binary Body

See more REST Examples

Demonstrates Rest.FullRequestBd, which sends a complete request whose binary body is read from a BinData and stores the text response body in a StringBuilder.

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. FullRequestBd is used when the request payload is binary, such as an image or other file, while the response is textual (for example a JSON acknowledgement).

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Boolean iBAutoReconnect
    Variant vBdRequest
    Handle hoBdRequest
    Boolean iBLoaded
    Variant vSbResponse
    Handle hoSbResponse
    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.

    //  FullRequestBd sends a request whose binary body is read from a BinData and stores the text
    //  response body in a StringBuilder.
    Get Create (RefClass(cComChilkatBinData)) To hoBdRequest
    If (Not(IsComObjectCreated(hoBdRequest))) Begin
        Send CreateComObject of hoBdRequest
    End
    Get ComLoadFile Of hoBdRequest "qa_data/image.png" To iBLoaded
    If (iBLoaded <> True) Begin
        Showln "Failed to load the request body file."
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "Content-Type" "image/png" To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponse
    If (Not(IsComObjectCreated(hoSbResponse))) Begin
        Send CreateComObject of hoSbResponse
    End
    Get pvComObject of hoBdRequest to vBdRequest
    Get pvComObject of hoSbResponse to vSbResponse
    Get ComFullRequestBd Of hoRest "PUT" "/api/images/1" vBdRequest vSbResponse To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetAsString Of hoSbResponse To sResponseText
    Get ComLastMethodSuccess Of hoSbResponse To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoSbResponse To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sResponseText


End_Procedure