Sample code for 30+ languages & platforms
PowerBuilder

Xero Get File Content (Files API)

Demonstrates how to download the content of a Xero file.

Note: This example requires Chilkat v9.5.0.64 or greater.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
string ls_FileID
oleobject loo_SbPath
integer li_NumReplaced
oleobject loo_JpgData
oleobject loo_SbErrorText

li_Success = 0

// Note: Requires Chilkat v9.5.0.64 or greater.

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

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Before sending REST API calls, the REST object needs to be
// initialized for OAuth1.
// See Xero 2-Legged OAuth1 Setup for sample code.

// Assuming the REST object's OAuth1 authenticator is setup, and the initial
// connection was made, we may now send REST HTTP requests..

// ------------------------------------------------------------------------------
ls_FileID = "f042e9a3-a31d-4595-b8b3-6030ea6084bb"

loo_SbPath = create oleobject
li_rc = loo_SbPath.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbPath.Append("/files.xro/1.0/Files/{FileId}/Content")
li_NumReplaced = loo_SbPath.Replace("{FileId}",ls_FileID)

loo_JpgData = create oleobject
li_rc = loo_JpgData.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Rest.FullRequestNoBodyBd("GET",loo_SbPath.GetAsString(),loo_JpgData)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_SbPath
    destroy loo_JpgData
    return
end if

// 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 loo_Rest.ResponseStatusCode <> 200 then
    loo_SbErrorText = create oleobject
    li_rc = loo_SbErrorText.ConnectToNewObject("Chilkat.StringBuilder")

    loo_SbErrorText.AppendBd(loo_JpgData,"utf-8",0,0)
    Write-Debug loo_SbErrorText.GetAsString()
    Write-Debug "-- Failed."
    destroy loo_Rest
    destroy loo_SbPath
    destroy loo_JpgData
    destroy loo_SbErrorText
    return
end if

// Save to a local file.
li_Success = loo_JpgData.WriteFile("qa_output/xero_penguins.jpg")
if li_Success <> 1 then
    Write-Debug "Failed to save to local file."
end if

Write-Debug "Xero Get File was Successful."


destroy loo_Rest
destroy loo_SbPath
destroy loo_JpgData
destroy loo_SbErrorText