Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkBinData.pb"

Procedure ChilkatExample()

    success.i = 0

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    bTls.i = 1
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"example.com",443,bTls,bAutoReconnect)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  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.
    bdResponse.i = CkBinData::ckCreate()
    If bdResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestNoBodyBd(rest,"GET","/api/images/1",bdResponse)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkBinData::ckDispose(bdResponse)
        ProcedureReturn
    EndIf

    bSaved.i = CkBinData::ckWriteFile(bdResponse,"qa_output/image.png")
    If bSaved <> 1
        Debug "Failed to save the response body."
        CkRest::ckDispose(rest)
        CkBinData::ckDispose(bdResponse)
        ProcedureReturn
    EndIf

    Debug "Downloaded " + Str(CkBinData::ckNumBytes(bdResponse)) + " bytes."


    CkRest::ckDispose(rest)
    CkBinData::ckDispose(bdResponse)


    ProcedureReturn
EndProcedure