Xero Upload Invoice file using API

34 Views Asked by At

I am trying to upload a file to an existing invoice and running into nothing but problems using the API. I can GET the files OK so my authentication is all OK. Below is my test code and result just saying there is a problem processing my request.

Is this a VBA code or a XERO API issue.

This is my test code to try an upload a very basic TXT file.


    Dim URL As String
    Dim http As Object
    Dim invfilepath As String
    Dim bdy As String

    Set http = CreateObject("MSXML2.XMLHTTP")
    guidtarget = "995f6b3e-085b-4ec7-82e5-f8b78235f8e3"   'GUID of the invoice

    URL = "https://api.xero.com/api.xro/2.0/Invoices/" & guidtarget & "/Attachments/test.txt"

    bdy = "This is a test"
    toke = Trim(DLookup("\[Access_token\]", "Xero_Tokens"))   'Gets the current token
    tid = get_registry("Tennant_ID", "")                    ' Gets the Tenant ID

    http.Open "POST", URL, False
    http.setRequestHeader "Authorization: ", "Bearer " & toke
    http.setRequestHeader "xero-tenant-id: ", tid
    http.setRequestHeader "Content Type:", "text/plain"
    http.Send bdy

    responsedata = http.responseText

    Me!resp = responsedata`


*****************************************************************************

When I run the code I get this

*****************************************************************************
An error occurred while processing your request.
Reference #253.95b92917.1711691972.45a51cbe
https://errors.edgesuite.net/253.95b92917.1711691972.45a51cbe
*****************************************************************************
1

There are 1 best solutions below

0
On

For anybody that comes across this, I ended up solving it. The Xero API documents are wrong in the CONTENT-TYPE to send as a header (and yes I had a syntax error for the content-type) Here is my code that finally worked

   Function XERO_Upload_InvPDF(dCounter)   ' Generic call for a POST to upload 
   PDF invoice to Xero
     Dim URL As String
     Dim http As Object
     Dim invfilepath As String
     Set http = CreateObject("MSXML2.XMLHTTP")
  
  crit = "dCounter = " & dCounter
  guidtarget = DLookup("dExportAccount", "Invoices", "dCounter = " & dCounter)
  pdfname = Invoices_PDFfileName(dCounter, False)

  URL = "https://api.xero.com/api.xro/2.0/Invoices/" & guidtarget & "/Attachments/" & pdfname


  invfilepath = Invoices_PDFfileName(dCounter)

  
    ' read file into pdfBinary
    Dim pdfBinary
    Dim ado As New ADODB.Stream
    ado.Type = 1 'binary
    ado.Open
    ado.LoadFromFile invfilepath
    ado.Position = 0
    pdfBinary = ado.Read
    ado.Close

  toke = Trim(DLookup("[Access_token]", "Xero_Tokens"))
  tid = get_registry("Tennant_ID", "")

  http.Open "PUT", URL, False
  http.setRequestHeader "Authorization: ", "Bearer " & toke
  http.setRequestHeader "xero-tenant-id: ", tid
  http.setRequestHeader "Content-Type:", "application/octet-stream"
  'http.setRequestHeader "Content-Type:", "application/pdf"
  http.setRequestHeader "Accept:", "application/json"
  http.Send pdfBinary
  responsedata = http.responseText
Debug.Print "*****************************************************************"
Debug.Print responsedata
  XERO_Upload_InvPDF = responsedata
End Function