Sample code for 30+ languages & platforms
DataFlex

Xero Upload Attachment

Upload an attachment. (POST an attachment).

Note: Requires Chilkat v9.5.0.64 or greater.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    String sEndPoint
    String sReceiptID
    String sFilename
    Handle hoSbPath
    Integer iNumReplaced
    Variant vJpgData
    Handle hoJpgData
    Variant vJpgDataFromWeb
    Handle hoJpgDataFromWeb
    Handle hoHttp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

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

    // --------------------------------------------------------------
    // This example will upload an image attachment to a receipt.

    Move "Receipts" To sEndPoint
    Move "c4f40e59-c390-0001-caff-ce731c707d00" To sReceiptID
    // This JPG image can be downloaded from https://www.chilkatsoft.com/syncedImages/penguins.jpg
    Move "penguins.jpg" To sFilename

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPath
    If (Not(IsComObjectCreated(hoSbPath))) Begin
        Send CreateComObject of hoSbPath
    End
    Get ComAppend Of hoSbPath "/api.xro/2.0/{Endpoint}/{Guid}/Attachments/{Filename}" To iSuccess
    Get ComReplace Of hoSbPath "{Endpoint}" sEndPoint To iNumReplaced
    Get ComReplace Of hoSbPath "{Guid}" sReceiptID To iNumReplaced
    Get ComReplace Of hoSbPath "{Filename}" sFilename To iNumReplaced

    Get ComAddHeader Of hoRest "Content-Type" "image/jpeg" To iSuccess

    // Load the JPG image from a file.
    Get Create (RefClass(cComChilkatBinData)) To hoJpgData
    If (Not(IsComObjectCreated(hoJpgData))) Begin
        Send CreateComObject of hoJpgData
    End
    Get ComLoadFile Of hoJpgData "qa_data/jpg/penguins.jpg" To iSuccess

    // We could alternatively get it from a URL like this:
    Get Create (RefClass(cComChilkatBinData)) To hoJpgDataFromWeb
    If (Not(IsComObjectCreated(hoJpgDataFromWeb))) Begin
        Send CreateComObject of hoJpgDataFromWeb
    End
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Get pvComObject of hoJpgDataFromWeb to vJpgDataFromWeb
    Get ComQuickGetBd Of hoHttp "https://www.chilkatsoft.com/syncedImages/penguins.jpg" vJpgDataFromWeb To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Upload with a POST
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get ComGetAsString Of hoSbPath To sTemp1
    Get pvComObject of hoJpgData to vJpgData
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComFullRequestBd Of hoRest "POST" sTemp1 vJpgData vSbResponseBody To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // A 200 response is expected for actual success.
    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComGetAsString Of hoSbResponseBody To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Examine the XML response
    Get ComGetAsString Of hoSbResponseBody To sTemp1
    Showln sTemp1

    // A successful response looks like this:

    // 	<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    // 	  <Id>d82568cd-d222-49e6-b713-880b6edb0606</Id>
    // 	  <Status>OK</Status>
    // 	  <ProviderName>ChilkatPrivate</ProviderName>
    // 	  <DateTimeUTC>2016-11-11T14:17:49.5922609Z</DateTimeUTC>
    // 	  <Attachments>
    // 	    <Attachment>
    // 	      <AttachmentID>0edcddc8-325f-40c7-b950-8c71f14afc7c</AttachmentID>
    // 	      <FileName>penguins.jpg</FileName>
    // 	      <Url>http://api.xero.com/api.xro/2.0/Receipts/c4f40e59-c390-0001-caff-ce731c707d00/Attachments/penguins.jpg</Url>
    // 	      <MimeType>image/jpg</MimeType>
    // 	      <ContentLength>777835</ContentLength>
    // 	    </Attachment>
    // 	  </Attachments>
    // 	</Response>


End_Procedure