Sample code for 30+ languages & platforms
DataFlex

REST Download Binary File to Memory

See more REST Examples

Download a binary file to a Chilkat BinData object.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    String sPathPartOfUrl
    String sDomain
    Boolean iBTls
    Integer iPort
    Boolean iBAutoReconnect
    Variant vBd
    Handle hoBd
    Handle hoSbErrorText
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    // We're going to download a sample MS-Word doc file.
    // The URLs of our MS-Word sample documents are:

    // https://www.chilkatdownload.com/sample_data/sample.doc
    // https://www.chilkatdownload.com/sample_data/sample.docx

    Move "/sample_data/sample.doc" To sPathPartOfUrl
    Move "chilkatdownload.com" To sDomain

    Move True To iBTls
    Move 443 To iPort
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest sDomain iPort iBTls iBAutoReconnect To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get pvComObject of hoBd to vBd
    Get ComFullRequestNoBodyBd Of hoRest "GET" sPathPartOfUrl vBd To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // A 200 response is expected for actual success.
    // If we don't get a 200 response, then the response body was not actually
    // the file data, but it was text containing error information.
    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 200) Begin
        Get Create (RefClass(cComChilkatStringBuilder)) To hoSbErrorText
        If (Not(IsComObjectCreated(hoSbErrorText))) Begin
            Send CreateComObject of hoSbErrorText
        End
        Get pvComObject of hoBd to vBd
        Get ComAppendBd Of hoSbErrorText vBd "utf-8" 0 0 To iSuccess
        Get ComGetAsString Of hoSbErrorText To sTemp1
        Showln sTemp1
        Showln "-- Failed."
        Procedure_Return
    End

    // Save to a local file.
    // Change the file path based on your operating system or needs...
    Get ComWriteFile Of hoBd "c:/temp/qa_output/sample.doc" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Failed to save to local file."
        Procedure_Return
    End

    Showln "REST Download of MS-Word File was successful."


End_Procedure