Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Web API Examples

Primary Categories

AWS Translate
Activix CRM
Adyen
Amazon MWS
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Bluzone
CardConnect
ClickBank
Clickatell
Cloudfare
DocuSign
ETrade
Etsy
Facebook
GeoOp
GetHarvest
Jira
MYOB

Magento
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Okta OAuth/OIDC
OneLogin OIDC
PayPal
Peoplevox
Populi
QuickBooks
SOAP finkok.com
Shippo
Shopify
Stripe
SugarCRM
Trello
Twitter
VoiceBase
Vonage
Walmart
Walmart v3
Xero
eBay
effectconnect

 

 

 

(DataFlex) Xero Upload File (Files API)

Demonstrates how to upload a file to a Xero folder.

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

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoRest
    Boolean iSuccess
    String sFolderID
    String sFilename
    Handle hoSbPath
    Integer iNumReplaced
    Variant vJpgData
    Handle hoJpgData
    Variant vJpgDataFromWeb
    Handle hoJpgDataFromWeb
    Handle hoHttp
    String sResponseJson
    Handle hoJson
    String sTemp1
    Integer iTemp1
    Integer iTemp2
    Boolean bTemp1

    // 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 a file to a folder using the Xero FILES API

    Move "0ffca059-f2f1-4271-8de9-4b87c8c2c638" To sFolderID
    // 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 "/files.xro/1.0/Files/{FolderId}" To iSuccess
    Get ComReplace Of hoSbPath "{FolderId}" sFolderID 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

    // 
    // Put the file data in the 1st sub-part in the multipart/form-data request we'll be sending.
    Set ComPartSelector Of hoRest To "1"
    Get pvComObject of hoJpgData to vJpgData
    Get ComSetMultipartBodyBd Of hoRest vJpgData To iSuccess

    // Set request headers in the 1st subpart (as indicated by the PartSelector)
    Get ComAddHeader Of hoRest "Content-Type" "image/jpeg" To iSuccess
    Get ComAddHeader Of hoRest "Content-Disposition" "multipart/form-data; name=Xero; filename=penguins.jpg" To iSuccess

    // Restore the PartSelector to an empty string.
    Set ComPartSelector Of hoRest To ""

    Get ComAddHeader Of hoRest "Content-Type" "multipart/form-data" To iSuccess

    // Upload with a multipart/form-data POST
    Get ComGetAsString Of hoSbPath To sTemp1
    Get ComFullRequestMultipart Of hoRest "POST" sTemp1 To sResponseJson
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComLoad Of hoJson sResponseJson To iSuccess
    Set ComEmitCompact Of hoJson To False

    // A 201 response is expected for actual success.
    // The Xero documentation doesn't explicitly state it, but that's what we've found in testing.
    // To be safe, we'll check for either 200 or 201.
    Get ComResponseStatusCode Of hoRest To iTemp1
    Get ComResponseStatusCode Of hoRest To iTemp2
    If ((iTemp1 <> 200) And (iTemp2 <> 201)) Begin
        Get ComEmit Of hoJson To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Examine the JSON response
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

    // A successful response looks like this:

    // 	{ 
    // 	  "Name": "penguins.jpg",
    // 	  "MimeType": "image/jpeg",
    // 	  "Size": 777835,
    // 	  "CreatedDateUtc": "2016-11-12T15:31:53.4230000",
    // 	  "UpdatedDateUtc": "2016-11-12T15:31:53.4230000",
    // 	  "User": { 
    // 	    "Name": "admin@chilkatsoft.com",
    // 	    "FirstName": "Matthew",
    // 	    "LastName": "Smith",
    // 	    "FullName": "Matthew Smith",
    // 	    "Id": "c362fe42-cb12-461f-b84a-c281c1a74841"
    // 	  },
    // 	  "FolderId": "0ffca059-f2f1-4271-8de9-4b87c8c2c638",
    // 	  "Id": "f042e9a3-a31d-4595-b8b3-6030ea6084bb"
    // 	}


End_Procedure

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.