+ Reply to Thread
Results 1 to 2 of 2

Using XMLHTTP to upload a file to API

Hybrid View

  1. #1
    Registered User
    Join Date
    06-15-2015
    Location
    Melbourne, Aus
    MS-Off Ver
    2013
    Posts
    2

    Using XMLHTTP to upload a file to API

    I am developing a tool to assist my sales team in the quotation process.

    Majority of our quotation is done in Excel, as part of when the quote is generated it saves the quote as a PDF and I would like it to automatically upload all the quote data and a copy of the PDF to our CRM package using it's API.

    I have done this using a VBA script and have managed to get everything to work except the file upload (so it creates a new quote, fills in the customer, items etc).

    The upload is done in two parts, first I must retrieve an attachment key, secondly I use that key to upload the file.

    The code I am using is as follows, the [ ]'s are where I've removed stuff:

    Public Function UploadQuote()
    
        Dim Req As New XMLHTTP
    
        ' retrieve attachment key    
        Req.Open "POST", [url], False, [username], [password]
        Req.setRequestHeader "Content-Type", "text/xml"
        Req.send ("<methodCall><methodName>QuoteRetrieveAttachmentKey</methodName>" & _
        "<params><param>" & [stuff here] & "</param></params></methodCall>")
        
        Dim AttachmentKey As String
        AttachmentKey = Req.responseXML.ChildNodes.Item(1).[few more].Text
          
        Dim FileName As String
        FileName = "C:\pdf-test.pdf" 'this is for my testing purposes
        
        ' upload the file  
        Req.Open "POST", [url], False, [username], [password]
        Req.setRequestHeader "Content-Type", "multipart/form-data; boundary=[boundary]"
        Req.send ("--[boundary]" & vbCrLf & _
        "Content-Disposition: form-data; name=""File""; filename=""" & FileName & """" & vbCrLf & _
        "Content-Type: application/pdf" & _
        vbCrLf & vbCrLf & _
    
        [DATA NEEDS TO GO HERE] & _
    
        vbCrLf & "[boundary]" & vbCrLf & _
        "Content-Disposition: form-data; name=""AttachmentKey""" & _
        vbCrLf & vbCrLf & AttachmentKey & vbCrLf & "[boundary]--")
        
        MsgBox Req.responseText
        
     End Function
    Using plain text I can upload a .txt file and that works fine (get success message and can access the file from the CRM).

    However I want to be uploading PDF files.

    When I try any of the methods I have been able to find online for converting the data, the upload 'is successful' yet the data when I access the file from the CRM package is corrupt (and a lot larger than the original file (20kbs become around 80kbs)).

    Using Postman I am able to upload the file so I know the API and process works. (If anyone knows what Postman does and convert that to VBA!)

    How / what am I meant to do to the file to get it into a form I can send the data via XMLHTTP??

    I have no idea what I am doing and everything I have managed to get working so far is already due to people online so thank you in advanced!

  2. #2
    Registered User
    Join Date
    06-15-2015
    Location
    Melbourne, Aus
    MS-Off Ver
    2013
    Posts
    2

    Re: Using XMLHTTP to upload a file to API

    Found an answer here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] cannot upload file
    By massive_set in forum Suggestions for Improvement
    Replies: 1
    Last Post: 01-14-2013, 03:52 AM
  2. vba upload file via cmd
    By akulka58 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-26-2012, 02:33 PM
  3. XMLHTTP.ResponseBody / XMLHTTP.Responsetext Paste into Excel Sheet
    By gophins in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-11-2012, 11:38 AM
  4. Help to upload an OLB file
    By rroma in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-18-2011, 10:20 AM
  5. [SOLVED] upload file name
    By choice in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-25-2005, 07:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1