Sample code for 30+ languages & platforms
Xbase++

REST Download Binary File to Memory

See more REST Examples

Download a binary file to a Chilkat BinData object.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL cPathPartOfUrl
LOCAL cDomain
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oBd
LOCAL oSbErrorText

nSuccess := 0

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

oRest := CreateObject("Chilkat.Rest")

//  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

cPathPartOfUrl := "/sample_data/sample.doc"
cDomain := "chilkatdownload.com"

nBTls := 1
nPort := 443
nBAutoReconnect := 1
nSuccess := oRest:Connect(cDomain, nPort, nBTls, nBAutoReconnect)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    RETURN
ENDIF

oBd := CreateObject("Chilkat.BinData")
nSuccess := oRest:FullRequestNoBodyBd("GET", cPathPartOfUrl, oBd)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oBd:destroy()
    RETURN
ENDIF

//  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.
IF (oRest:ResponseStatusCode != 200)
    oSbErrorText := CreateObject("Chilkat.StringBuilder")
    oSbErrorText:AppendBd(oBd, "utf-8", 0, 0)
    ? oSbErrorText:GetAsString()
    ? "-- Failed."
    oRest:destroy()
    oBd:destroy()
    oSbErrorText:destroy()
    RETURN
ENDIF

//  Save to a local file.
//  Change the file path based on your operating system or needs...
nSuccess := oBd:WriteFile("c:/temp/qa_output/sample.doc")
IF (nSuccess != 1)
    ? "Failed to save to local file."
    oRest:destroy()
    oBd:destroy()
    oSbErrorText:destroy()
    RETURN
ENDIF

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

oRest:destroy()
oBd:destroy()
oSbErrorText:destroy()