Sample code for 30+ languages & platforms
Xbase++

HTTP Download to Stream

See more REST Examples

Demonstrates how to stream the response body directly to a stream, which in this case is to a file stream.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL oUrl
LOCAL nBAutoReconnect
LOCAL nResponseStatusCode
LOCAL oBodyStream
LOCAL cErrResponse

nSuccess := 0

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

oRest := CreateObject("Chilkat.Rest")

//  The URL we want to download is: 
//  https://github.com/chilkatsoft/OAuth2-CSharp-Desktop/archive/master.zip
oUrl := CreateObject("Chilkat.Url")
oUrl:ParseUrl("https://github.com/chilkatsoft/OAuth2-CSharp-Desktop/archive/master.zip")

//  Connect to the web server
nBAutoReconnect := 1
nSuccess := oRest:Connect(oUrl:Host, oUrl:Port, oUrl:Ssl, nBAutoReconnect)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oUrl:destroy()
    RETURN
ENDIF

nSuccess := oRest:SendReqNoBody("GET", oUrl:Path)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oUrl:destroy()
    RETURN
ENDIF

//  Read the response header.
nResponseStatusCode := oRest:ReadResponseHeader()
IF (nResponseStatusCode < 0)
    ? oRest:LastErrorText
    oRest:destroy()
    oUrl:destroy()
    RETURN
ENDIF

? "Response status code = " + Str(nResponseStatusCode)

//  We expect a 200 response status if the file data is coming.
//  Otherwise, we'll get a string response body with an error message(or no response body).
IF (nResponseStatusCode == 200)

    oBodyStream := CreateObject("Chilkat.Stream")

    //  The stream's sink will be a file.
    oBodyStream:SinkFile := "qa_output/OAuth2-CSharp-Desktop/archive/master.zip"

    //  Read the response body to the stream.  Given that we've
    //  set the stream's sink to a file, it will stream directly
    //  to the file.
    nSuccess := oRest:ReadRespBodyStream(oBodyStream, 1)
    IF (nSuccess != 1)
        ? oRest:LastErrorText
        oRest:destroy()
        oUrl:destroy()
        oBodyStream:destroy()
        RETURN
    ENDIF

    ? "Successfully downloaded the file."

ELSE
    cErrResponse := oRest:ReadRespBodyString()
    IF (oRest:LastMethodSuccess != 1)
        ? oRest:LastErrorText
    ELSE
        ? cErrResponse
    ENDIF

ENDIF

oRest:destroy()
oUrl:destroy()
oBodyStream:destroy()