Sample code for 30+ languages & platforms
DataFlex

Download a Binary REST Response into BinData

See more REST Examples

Demonstrates Rest.FullRequestNoBodyBd, which sends a request with no body and stores the binary response body in 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. This method is appropriate when the response is binary, such as an image or file download, allowing the received bytes to be saved or processed directly.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Boolean iBAutoReconnect
    Variant vBdResponse
    Handle hoBdResponse
    Boolean iBSaved
    String sTemp1
    Integer iTemp1

    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.

    //  FullRequestNoBodyBd sends a request with no body and stores the binary response body in a
    //  BinData.  This is appropriate when the response is binary, such as an image or file download.
    Get Create (RefClass(cComChilkatBinData)) To hoBdResponse
    If (Not(IsComObjectCreated(hoBdResponse))) Begin
        Send CreateComObject of hoBdResponse
    End
    Get pvComObject of hoBdResponse to vBdResponse
    Get ComFullRequestNoBodyBd Of hoRest "GET" "/api/images/1" vBdResponse To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComWriteFile Of hoBdResponse "qa_output/image.png" To iBSaved
    If (iBSaved <> True) Begin
        Showln "Failed to save the response body."
        Procedure_Return
    End

    Get ComNumBytes Of hoBdResponse To iTemp1
    Showln "Downloaded " iTemp1 " bytes."


End_Procedure