Sample code for 30+ languages & platforms
Xbase++

REST Read Binary Response into BinData Object (Amazon S3)

Demonstrates how to make a REST call that returns a binary response body. This example is valid for files that fit in memory. The example shows how to download a JPG from the Amazon S3 service.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oAuthAws
LOCAL nResponseStatusCode
LOCAL cErrResponse
LOCAL oBdBody

nSuccess := 0

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

oRest := CreateObject("Chilkat.Rest")

//  Connect to the Amazon AWS REST server.
nBTls := 1
nPort := 443
nBAutoReconnect := 1
nSuccess := oRest:Connect("s3.amazonaws.com", nPort, nBTls, nBAutoReconnect)

//  ----------------------------------------------------------------------------
//  Important: For buckets created in regions outside us-east-1,
//  there are three important changes that need to be made.
//  See Working with S3 Buckets in Non-us-east-1 Regions for the details.
//  ----------------------------------------------------------------------------

//  Provide AWS credentials for the REST call.
oAuthAws := CreateObject("Chilkat.AuthAws")
oAuthAws:AccessKey := "AWS_ACCESS_KEY"
oAuthAws:SecretKey := "AWS_SECRET_KEY"
oAuthAws:ServiceName := "s3"
nSuccess := oRest:SetAuthAws(oAuthAws)

//  Set the bucket name via the HOST header.
//  In this case, the bucket name is "chilkat100".
oRest:Host := "chilkat100.s3.amazonaws.com"

//  Send the request to download the JPG.
nSuccess := oRest:SendReqNoBody("GET", "/starfish.jpg")
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oAuthAws:destroy()
    RETURN
ENDIF

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

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

//  We expect a 200 response status if the JPG data is coming.
//  Otherwise, we'll get a string response body with an error message(or no response body).
IF (nResponseStatusCode != 200)
    cErrResponse := oRest:ReadRespBodyString()
    IF (oRest:LastMethodSuccess != 1)
        ? oRest:LastErrorText
    ELSE
        ? cErrResponse
    ENDIF

    oRest:destroy()
    oAuthAws:destroy()
    RETURN
ENDIF

oBdBody := CreateObject("Chilkat.BinData")
nSuccess := oRest:ReadRespBd(oBdBody)
IF (nSuccess == 0)
    ? oRest:LastErrorText
    oRest:destroy()
    oAuthAws:destroy()
    oBdBody:destroy()
    RETURN
ENDIF

//  Save the contents of the response body to a file.
nSuccess := oBdBody:WriteFile("qa_output/starfish.jpg")
IF (nSuccess == 1)
    ? "Success"
ELSE
    ? "Failed to write file."
ENDIF

oRest:destroy()
oAuthAws:destroy()
oBdBody:destroy()