Xbase++ Requires Chilkat v11.0.0+
Xbase++
Xero List Attachments for an Item (such as an Invoice)
See more Xero Examples
Demonstrates how to get information about the attachments for a particular Xero item.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oJsonToken
LOCAL cUrl
LOCAL oResp
LOCAL oJsonResponse
LOCAL cAttachmentID
LOCAL cFileName
LOCAL cV_Url
LOCAL cMimeType
LOCAL nContentLength
LOCAL cId
LOCAL cStatus
LOCAL cProviderName
LOCAL cDateTimeUTC
LOCAL i
LOCAL nCount_i
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oHttp := CreateObject("Chilkat.Http")
oJsonToken := CreateObject("Chilkat.JsonObject")
nSuccess := oJsonToken:LoadFile("qa_data/tokens/xero-access-token.json")
IF (nSuccess == 0)
? oJsonToken:LastErrorText
oHttp:destroy()
oJsonToken:destroy()
RETURN
ENDIF
oHttp:AuthToken := oJsonToken:StringOf("access_token")
// Replace the value here with an actual tenant ID obtained from this example:
// Get Xero Tenant IDs
oHttp:SetRequestHeader("Xero-tenant-id", "83299b9e-5747-4a14-a18a-a6c94f824eb7")
oHttp:Accept := "application/json"
cUrl := "https://api.xero.com/api.xro/2.0/{$Endpoint}/{$Guid}/Attachments/"
// Endpoint can be Invoices, Receipts, CreditNotes, PurchaseOrders, etc.
oHttp:SetUrlVar("Endpoint", "Invoices")
// Guid is the ID of the item, such as the InvoiceID.
oHttp:SetUrlVar("Guid", "0032f627-3156-4d30-9b1c-4d3b994dc921")
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpNoBody("GET", cUrl, oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oHttp:destroy()
oJsonToken:destroy()
oResp:destroy()
RETURN
ENDIF
? "Response Status Code: " + Str(oResp:StatusCode)
oJsonResponse := CreateObject("Chilkat.JsonObject")
oJsonResponse:Load(oResp:BodyStr)
oJsonResponse:EmitCompact := 0
? oJsonResponse:Emit()
IF (oResp:StatusCode != 200)
? "Failed."
oHttp:destroy()
oJsonToken:destroy()
oResp:destroy()
oJsonResponse:destroy()
RETURN
ENDIF
// Sample response:
//
// Use the this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// {
// "Id": "24bfbcb9-dec9-4d33-835c-8f165d776766",
// "Status": "OK",
// "ProviderName": "Chilkat2222",
// "DateTimeUTC": "\/Date(1587213296972)\/",
// "Attachments": [
// {
// "AttachmentID": "daf106e2-8634-4349-bfcc-86c1df0793b2",
// "FileName": "penguins.jpg",
// "Url": "https://api.xero.com/api.xro/2.0/Invoices/0032f627-3156-4d30-9b1c-4d3b994dc921/Attachments/penguins.jpg",
// "MimeType": "image/jpg",
// "ContentLength": 777835
// }
// ]
// }
cId := oJsonResponse:StringOf("Id")
cStatus := oJsonResponse:StringOf("Status")
cProviderName := oJsonResponse:StringOf("ProviderName")
cDateTimeUTC := oJsonResponse:StringOf("DateTimeUTC")
i := 0
nCount_i := oJsonResponse:SizeOfArray("Attachments")
DO WHILE i < nCount_i
oJsonResponse:I := i
cAttachmentID := oJsonResponse:StringOf("Attachments[i].AttachmentID")
cFileName := oJsonResponse:StringOf("Attachments[i].FileName")
cV_Url := oJsonResponse:StringOf("Attachments[i].Url")
cMimeType := oJsonResponse:StringOf("Attachments[i].MimeType")
nContentLength := oJsonResponse:IntOf("Attachments[i].ContentLength")
i := i + 1
ENDDO
oHttp:destroy()
oJsonToken:destroy()
oResp:destroy()
oJsonResponse:destroy()