I am attempting to import a CSV file using the Bulk Leads Import API with Classic ASP (VB Script). I have tried reading the CSV file into a string and including in the POST payload, as well as attaching the CSV file.
No matter what I do, I get an "Invalid Content Type."
{"requestId":"15f34#15ef18d4773","success":false,"errors":[{"code":"612","message":"Invalid Content Type"}]}
I have tried 5-10 different content types. I get the same error when I attempt to attach the CSV file. Anyone know how I can fix the error?
Also, if I use the method of reading the CSV into a string, am I adding the line termination correctly (cbCRLF or CHR(13)&CHR(10))
----------------------------------------------
Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
xmlHTTP.Open "POST", "https://REDACTED.mktorest.com/bulk/v1/leads.json?format=CSV&lookupField=email&access_token=REDACTED", false
xmlHTTP.setRequestHeader "content-type", "text/csv"
'I have also tried these content-types:
' application/x-www-form-urlencoded
' application/vnd.ms-excel
' application/json
' text/plain
' application/csv
' multipart/form-data
dim strData, strLine
strData = ""
strData=strData & "------WebKitFormBoundaryBQACkJZyaiIAXogC" & vbCrLf
strData=strData & "Content-Disposition: form-data; name=""file""; filename=""leads.csv""" & vbCrLf
'strData=strData & "Content-Disposition: attachment;filename=MarketoProspects.csv" 'I have also tried attaching the CSV file
strData=strData & "FirstName,LastName,Email,Company" & vbCrLf 'vbCRLF=Chr(13)&Chr(10)
strData=strData & "Able,Baker,ablebaker@marketo.com,Marketo" & vbCrLf
strData=strData & "Charlie,Dog,charliedog@marketo.com,Marketo" & vbCrLf
strData=strData & "Easy,Fox,easyfox@marketo.com,Marketo" & vbCrLf
strData=strData & "------WebKitFormBoundaryBQACkJZyaiIAXogC--"
xmlHTTP.SetRequestHeader "Content-Length", Len(strData) + 2
'strData = Server.URLEncode(strData) 'This is used if content-type=application/x-www-form-urlencoded
xmlHTTP.Send strData
I finally figured it out. My updated code is below with the solution in ALL CAPS.
Const Boundary="SOME_RANDOM_STRING" 'NOTICE THAT BOUNDARY DOES NOT HAVE ANY LEADING DASHES
Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
xmlHTTP.Open "POST", "https://REDACTED.mktorest.com/bulk/v1/leads.json?format=CSV&lookupField=email&access_token=REDACTED", false
xmlHTTP.setRequestHeader "content-type", "multipart/form-data; boundary=" & Boundary 'BOUNDARY MUST BE INCLUDED IN CONTENT TYPE
dim strData, strLine
strData = ""
strData=strData & "--" & Boundary & vbCrLf 'ADD 2 LEADING DASHES TO THE BOUNDARY
strData=strData & "Content-Disposition: form-data; name=""file""; filename=""leads.csv""" & vbCrLf
strData=strData & vbCrLf 'BLANK LINE AFTER CONTENT-DISPOSITION LINE
'ADD THE CONTENT OF THE CSV FILE IN THE POST PAYLOAD
dim objFSO, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(server.mappath("FileToUpload.csv"), 1) '1=ForReading
Const MaxRows=20 'DONE ONLY FOR TESTING
i=0
Do Until objFile.AtEndOfStream
i=i+1
if i>MaxRows then Exit Do
strLine = objFile.ReadLine
strData=strData & strLine & vbCrLf
Loop
strData=strData & "--" & Boundary & "--" 'ADD 2 LEADING AND 2 TRAILING DASHES TO THE BOUNDARY
xmlHTTP.SetRequestHeader "Content-Length", Len(strData) + 2
xmlHTTP.Send strData