Invalid credentials error when attempting PUT to a HTTPS site - vba

I am attempting to submit a file on a HTTPS site using VBA, but I am having issues with the authentication. (When viewed, the site has the standard field for file name, with a "browse" button, and a "submit" button.)
I've tried a couple of things... first, I used an InternetExplorer.Application object, but the element type that I need to populate is file, and I've read that this is not directly accessible via code for security reasons. (Sorry I don't have the link for a citation...)
Next suggestion was to use a WinHttp.WinHttpRequest.5.1 object and a PUT request. When I do that however, the response from the site is a 401, invalid authentication error.
I'm able to access the site without entering any credentials when I'm browsing normally. I've looked at some questions about HTTPS headers here and here, but haven't been able to get them to work. Can anyone see what I'm doing wrong?
Dim objHTTP As Object
Dim URL As String
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "https://siteImUploadingTo.domain.com/site"
objHTTP.Open "PUT", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.Send ("_fileToPost=" & ThisWorkbook.Path & \filename.PDF&_pagesSelection=1-100")
Debug.Print objHTTP.ResponseText 'returns a 401 invalid credentials error.

Looking at your code, it appears that you're missing a .SetCredentials call, after .Open and before .Send:
objHTTP.SetCredentials username, password, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
I ran your code on my test environment, and I also had to set the WinHttpRequestOption_SslErrorIgnoreFlags option to be able to ignore all SSL errors (reference):
objHTTP.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = &H3300 //SslErrorFlag_Ignore_All
At last, I don't think your Send command will work at actually posting a file to your server. I recommend you using the code below, adapted from this blog post.
' add a reference to "Microsoft WinHTTP Services, version 5.1"
Public Function PostFile( _
sUrl As String, sFileName As String, sUsername As String, sPassword As String, _
Optional bIgnoreAllSslErrors As Boolean = False, Optional bAsync As Boolean _
) As String
Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
Const STR_BOUNDARY As String = "3fbd04f5-b1ed-4060-99b9-fca7ff59c113"
Dim nFile As Integer
Dim baBuffer() As Byte
Dim sPostData As String
Dim browser As WinHttp.WinHttpRequest
'--- read file
nFile = FreeFile
Open sFileName For Binary Access Read As nFile
If LOF(nFile) > 0 Then
ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
Get nFile, , baBuffer
sPostData = StrConv(baBuffer, vbUnicode)
End If
Close nFile
'--- prepare body
sPostData = _
"--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
"Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
sPostData & vbCrLf & _
"--" & STR_BOUNDARY & "--"
'--- post
Set browser = New WinHttpRequest
browser.Open "POST", sUrl, bAsync
browser.SetCredentials sUsername, sPassword, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
If bIgnoreAllSslErrors Then
' https://stackoverflow.com/questions/12080824/how-to-ignore-invalid-certificates-with-iwinhttprequest#12081003
browser.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = &H3300
End If
browser.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
browser.Send pvToByteArray(sPostData)
If Not bAsync Then
PostFile = browser.ResponseText
End If
End Function
Private Function pvToByteArray(sText As String) As Byte()
pvToByteArray = StrConv(sText, vbFromUnicode)
End Function
If you need to send additional fields, you can do so by modifying the sPostData variable:
sPostData = _
"--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data; name=""field1""" & vbCrLf & vbCrLf & _
field1 & vbCrLf & _
"--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data; name=""field2""" & vbCrLf & vbCrLf & _
field2 & vbCrLf & _
"--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(FileFullPath, InStrRev(FileFullPath, "\") + 1) & """" & vbCrLf & _
"Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
sPostData & vbCrLf & _
"--" & STR_BOUNDARY & "--"

Related

How do I upload a photo from my local machine to a Facebook page using the Facebook Graph API (via Access VBA)

I want to upload a photo from my local machine (e.g. a jpg) to a Facebook page I am the admin on, using the Facebook Graph API (using Access VBA).
I can post an image that is on the internet already to the Facebook page, with a message, that works fine.
But I want the file to come from my local machine...
I can't work it out!
This code (VBA) works to upload a JPG that is already on the internet
Dim httpRequest As Object
Dim boundary As String
Dim postData As String
Dim pageID As String, accessToken As String, fileUrl As String, message As String
pageID = "[My Page ID]"
accessToken = "[My long-lived Access Token]"
fileUrl = "https://www.facebook.com/images/fb_icon_325x325.png"
message = "test message"
boundary = "----------------------------" & Format(Now, "ddmmyyyyhhmmss")
postData = "--" & boundary & vbCrLf & _
"Content-Disposition: form-data; name=""message""" & vbCrLf & _
"Content-Type: text/plain; charset=UTF-8" & vbCrLf & vbCrLf & _
message & vbCrLf & _
"--" & boundary & vbCrLf & _
"Content-Disposition: form-data; name=""url""" & vbCrLf & _
"Content-Type: text/plain; charset=UTF-8" & vbCrLf & vbCrLf & _
fileUrl & vbCrLf & _
"--" & boundary & "--"
Set httpRequest = CreateObject("MSXML2.XMLHTTP")
With httpRequest
.Open "POST", "https://graph.facebook.com/" & pageID & "/photos?access_token=" & accessToken, False
.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
.send (postData)
If .status = 200 Then
Debug.Print .responseText
Else
Debug.Print "Error: " & .status & " - " & .statusText
End If
End With
This works.
I can't for the life of me work out how to change the photo to be one from my local machine though.
Please note: I have the correct token settings because, as I say, this code above works for a photo that is already on the internet - so my page ID and my access token must be correct.
[My text logical question will be.... how do I upload multiple photos?]
Thanks for any help you can offer!
A URL like this should work:
file:///C:/Test/YourImage.jpg
Note the forward slashes, though you may get away with backslashes in the filename part.
OK I managed to get this working:
Public Function UploadFileToFacebookPage()
Dim httpRequest As Object
Dim boundary As String
Dim postData As String
Dim pageID As String, accessToken As String, fileUrl As String, message As String
pageID = "[My page ID]"
accessToken = "[My long-lived access token]"
Dim fileName As String, filePath As String
filePath = "Z:\Desktop\testimage.jpg"
fileName = "testimage.jpg"
message = "test message"
boundary = "----------------------------" & Format(Now, "ddmmyyyyhhmmss")
postData = "--" & boundary & vbCrLf
postData = postData & "Content-Disposition: form-data; name=""message""" & vbCrLf
postData = postData & "Content-Type: text/plain; charset=UTF-8" & vbCrLf & vbCrLf
postData = postData & message & vbCrLf
postData = postData & "--" & boundary & vbCrLf
postData = postData & "Content-Disposition: form-data; name=""source""; filename=""" & fileName & """" & vbCrLf
postData = postData & "Content-Type: image/jpeg" & vbCrLf & vbCrLf
postData = postData & getBinaryFile(filePath) & vbCrLf
postData = postData & "--" & boundary & "--" & vbCrLf
Set httpRequest = CreateObject("MSXML2.XMLHTTP")
With httpRequest
.Open "POST", "https://graph.facebook.com/" & pageID & "/photos?access_token=" & accessToken, False
.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
.send ToByteArray(postData) ' Convert from Unicode
If .Status = 200 Then
Debug.Print .ResponseText
Else
Debug.Print "Error: " & .Status & " - " & .statusText
End If
End With
End Function
and
Function getBinaryFile(filePath) As Variant
Dim binaryStream As Variant
Set binaryStream = CreateObject("ADODB.Stream")
binaryStream.Type = 1
binaryStream.Open
binaryStream.LoadFromFile filePath
getBinaryFile = StrConv(binaryStream.Read, vbUnicode) ' Convert to Unicode
binaryStream.Close
End Function
and
Function ToByteArray(str As String) As Byte()
ToByteArray = StrConv(str, vbFromUnicode)
End Function

VBA send file in binary code to API via POST method

I have last problem with my code. Code sending via POST variables from Outlook to API.
My last problem is how to send variables and mail attachment in one POST request to API.
first 7zip comprimation for mail attachement:
strSource = cstrFileAttachment & "*.*"
strTarget = cstrFileattachment & "Zip\attachment.zip"
strPassword = randomPassword(cintLongPassword)
strCommand = """" & PathZipProgram & """ a -tzip """ & strTarget & _
""" -p" & strPassword & " """ & strSource & """"
Now i have c:\attachment\attachment.zip
Next part is send variables to API:
Dim SendDataToApi As String
strFrom = 1
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "https://url.domain.com/api/data"
objHTTP.Open "POST", URL, False
objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
SendDataToApi = "mail_from=" & strFrom & "&mail_to=" & strKomu & "&file_attachment=" & fileAttachment & "&url_attribute=" & strWebLink & "&sms_code=" & strHeslo & "&id_message=" & IdMessage & "&mobile_phone=" & strPhone & "&date_send=" & strDateSend & "&date_expiration=" & strDateExp
objHTTP.Send SendDataToApi
Variables are sended, but fileAttachment is send as a string, so API get path where file is saved.
My question is how implement code below (found on internet) to my code sendDataToApi and POST attachment.zip as a binary insteed of string.
Private Function Upload(strUploadUrl, strFilePath, strFileField, strDataPairs)
'Uses POST to upload a file and miscellaneous form data
strUploadUrl = "https://url.domain.com/api/data"
strFilePath = cstrFilepathAttachment & "Zip\attachment.zip"
'strFileField is the web page equivalent form field name for the file (File1)
'strDataPairs are pipe-delimited form data pairs (foo=bar|snap=crackle)
Const MULTIPART_BOUNDARY = "---------------------------0123456789012"
Dim ado, rs
Dim lngCount
Dim bytFormData, bytFormStart, bytFormEnd, bytFile
Dim strFormStart, strFormEnd, strDataPair
Dim web
Const adLongVarBinary = 205
'Read the file into a byte array
Set ado = CreateObject("ADODB.Stream")
ado.Type = 1
ado.Open
ado.LoadFromFile strFilePath
bytFile = ado.Read
ado.Close
'Create the multipart form data.
'Define the end of form
strFormEnd = vbCrLf & "--" & MULTIPART_BOUNDARY & "--" & vbCrLf
'First add any ordinary form data pairs
strFormStart = ""
For Each strDataPair In Split(strDataPairs, "|")
strFormStart = strFormStart & "--" & MULTIPART_BOUNDARY & vbCrLf
strFormStart = strFormStart & "Content-Disposition: form-data; "
strFormStart = strFormStart & "name=""" & Split(strDataPair, "=")(0) & """"
strFormStart = strFormStart & vbCrLf & vbCrLf
strFormStart = strFormStart & Split(strDataPair, "=")(1)
strFormStart = strFormStart & vbCrLf
Next
'Now add the header for the uploaded file
strFormStart = strFormStart & "--" & MULTIPART_BOUNDARY & vbCrLf
strFormStart = strFormStart & "Content-Disposition: form-data; "
strFormStart = strFormStart & "name=""" & strFileField & """; "
strFormStart = strFormStart & "filename=""" & Mid(strFilePath, InStrRev(strFilePath, "\") + 1) & """"
strFormStart = strFormStart & vbCrLf
strFormStart = strFormStart & "Content-Type: application/upload" 'bogus, but it works
strFormStart = strFormStart & vbCrLf & vbCrLf
'Create a recordset large enough to hold everything
Set rs = CreateObject("ADODB.Recordset")
rs.Fields.Append "FormData", adLongVarBinary, Len(strFormStart) + LenB(bytFile) + Len(strFormEnd)
rs.Open
rs.AddNew
'Convert form data so far to zero-terminated byte array
For lngCount = 1 To Len(strFormStart)
bytFormStart = bytFormStart & ChrB(Asc(Mid(strFormStart, lngCount, 1)))
Next
rs("FormData").AppendChunk bytFormStart & ChrB(0)
bytFormStart = rs("formData").GetChunk(Len(strFormStart))
rs("FormData") = ""
'Get the end boundary as a zero-terminated byte array
For lngCount = 1 To Len(strFormEnd)
bytFormEnd = bytFormEnd & ChrB(Asc(Mid(strFormEnd, lngCount, 1)))
Next
rs("FormData").AppendChunk bytFormEnd & ChrB(0)
bytFormEnd = rs("formData").GetChunk(Len(strFormEnd))
rs("FormData") = ""
'Now merge it all
rs("FormData").AppendChunk bytFormStart
rs("FormData").AppendChunk bytFile
rs("FormData").AppendChunk bytFormEnd
bytFormData = rs("FormData")
rs.Close
'Upload it
Set web = CreateObject("WinHttp.WinHttpRequest.5.1")
web.Open "POST", strUploadUrl, False
web.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & MULTIPART_BOUNDARY
web.Send bytFormData
End Function
UPDATE:
when i added part of code from #Tim Williams
in my database is saved file as /tmp/phpAJOtVw what do i doing wrong ?
Upload is a standalone method, so you should be able to call it something like this:
sUrl = "https://url.domain.com/api/data" 'API endpoint
fPath = "c:\attachment\attachment.zip" 'attachment location
FileFieldName = "checkYourApiForThis" 'API specifies this
DataPairs = "mail_from=" & strFrom & _
"&mail_to=" & strKomu & _
"&file_attachment=" & fileAttachment & _
"&url_attribute=" & strWebLink & _
"&sms_code=" & strHeslo & _
"&id_message=" & IdMessage & _
"&mobile_phone=" & strPhone & _
"&date_send=" & strDateSend & _
"&date_expiration=" & strDateExp
'call the function
'expects |-delimited name/value pairs, not &, so do a replace
Upload sUrl, fPath, FileFieldName, Replace(DataPairs, "&", "|")
You should remove these hard-coded values from the top of Upload:
strUploadUrl = "https://url.domain.com/api/data"
strFilePath = cstrFilepathAttachment & "Zip\attachment.zip"

Out Of Memory (7) when upload file in VB6 using MSXML

i have a function to upload multipart/form-data with Visual Basic 6 using MSXML2.ServerXMLHTTP60, no problem with 100MB file size, but when i upload 200MB it's show "Run Time Error '7'" Out Of Memory.
this is my code:
Public Function PostFile(sUrl As String, sJSON As String, sFileName As String) As String
Const STR_BOUNDARY As String = "864d391d-4097-44e0-92e1-71aff17094c1"
Dim sPostData As String
Dim bytData
With CreateObject("ADODB.Stream")
.Type = 1
.Mode = 3
.Open
.LoadFromFile sFileName
bytData = .Read
End With
With CreateObject("ADODB.Stream")
.Mode = 3
.Charset = "Windows-1252"
.Open
.Type = 2
.WriteText "--" & STR_BOUNDARY & vbCrLf
.WriteText "Content-Disposition: form-data; name=""json""" & vbCrLf
.WriteText "Content-Type: application/json" & vbCrLf & vbCrLf
.WriteText sJSON & vbCrLf
.WriteText "--" & STR_BOUNDARY & vbCrLf
.WriteText "Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf
.WriteText "Content-Type: application/octet-stream" & vbCrLf & vbCrLf
.Position = 0
.Type = 1
.Position = .Size
.Write bytData
.Position = 0
.Type = 2
.Position = .Size
.WriteText vbCrLf & "--" & STR_BOUNDARY & "--"
.Position = 0
.Type = 1
sPostData = StrConv(.Read, vbUnicode)
End With
With New MSXML2.ServerXMLHTTP60
.Open "POST", sUrl, True
.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
.send ToByteArray(sPostData)
.waitForResponse 300 'second
If .Status = 200 Then PostFile = .responseText Else .abort
End With
End Function
Private Function ToByteArray(sText As String) As Byte()
ToByteArray = StrConv(sText, vbFromUnicode)
End Function
Before i update the script above i using "open file method" to read binary file like below:
Public Function PostFile(sUrl As String, sJSON As String, sFileName As String) As String
Const STR_BOUNDARY As String = "864d391d-4097-44e0-92e1-71aff17094c1"
Dim nFile As Integer
Dim baBuffer() As Byte
Dim sPostData As String
nFile = FreeFile
Open sFileName For Binary Access Read As nFile
If LOF(nFile) > 0 Then
ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
Get nFile, , baBuffer
sPostData = StrConv(baBuffer, vbUnicode)
End If
Close nFile
'--- prepare body
sPostData = "--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data; name=""json""" & vbCrLf & _
"Content-Type: application/json" & vbCrLf & vbCrLf & _
sJSON & vbCrLf & _
"--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
"Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
sPostData & vbCrLf & _
"--" & STR_BOUNDARY & "--"
'--- post
With New MSXML2.ServerXMLHTTP60
.Open "POST", sUrl, True
.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
.send ToByteArray(sPostData)
.waitForResponse 300 'second
If .Status = 200 Then PostFile = .responseText Else .abort
End With
End Function
Private Function ToByteArray(sText As String) As Byte()
ToByteArray = StrConv(sText, vbFromUnicode)
End Function
But, that show error "Run Time Error '14'" Out of string space
how to handle this error?

VBA - Named argument not found

Checking out https://wqweto.wordpress.com/2011/07/12/vb6-using-wininet-to-post-binary-file/ I get it how to upload a file, but I still need to add some additional parameters along the request, namely, _ID where this file should be put on server.
Currently I'm using code from first link:
Const STR_BOUNDARY As String = "3fbd04f5-b1ed-4060-99b9-fca7ff59c113"
Dim nFile As Integer
Dim baBuffer() As Byte
Dim sPostData As String
Dim pvPostFile As String
'--- read file
nFile = FreeFile
Open strPathFile For Binary Access Read As nFile
If LOF(nFile) > 0 Then
ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
Get nFile, , baBuffer
sPostData = StrConv(baBuffer, vbUnicode)
End If
Close nFile
'--- prepare body
sPostData = "--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data; name=""uploadfile""; filename=""a.xls""" & vbCrLf & _
"Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
sPostData & vbCrLf & _
"--" & STR_BOUNDARY & "--" & _
"--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data; name=""_id"";" & vbCrLf & vbCrLf & _
"BFKrhMovy25DDemGE" & _
"--" & STR_BOUNDARY & "--"
'--- post
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "POST", "https://api.backend.org/api/v1/upload/", False
.SetRequestHeader "Authorization: Bearer FbKl4KmuSdiXlk6PsWbvs9"
.SetRequestHeader "Content-Type: multipart/form-data; boundary=" & STR_BOUNDARY
.Send pvToByteArray(sPostData)
pvPostFile = .ResponseText
End With
MsgBox pvPostFile
Private Function pvToByteArray(sText As String) As Byte()
pvToByteArray = StrConv(sText, vbFromUnicode)
End Function
I'm getting "Named argument not found" in section
With CreateObject("WinHttp.WinHttpRequest.5.1")
not sure how to debug this :(
I have checked reference to Microsoft WinHTTP Services, version 5.1.
EDIT:
Problem was in SetRequestHeader, it needs two parameters following.
So correct code for SetRequestHeader lines is:
.SetRequestHeader "Authorization", "Bearer FbKl4KmuSdiXlk6PsWbvs9"
.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
Main problem for me was understanding how parameters work in VBA, coming from total PHP environment..
So correctly setting .SetRequestHeader parameters and correctly putting together custom parameter with using two vbCrLf made all the difference.
With this solution, you can send as many params as you want now..

Upload pdf via multipart-HTML-Post does change file

I try to upload a pdf via multipart with vba using this code:
Public Function sap_upload(ByVal par_objectID As String, ByVal par_description As String, ByVal par_filename As String) As Integer
Dim ls_param As String
Dim text As String
Dim line As String
Dim url As String
Dim web As MSXML2.XMLHTTP60
url = "http://someurl.xml"
Set web = CreateObject("MSXML2.XMLHTTP")
Call web.Open("POST", url, False)
Const Boundary As String = "AaB03x"
Call web.setRequestHeader("content-type", "multipart/form-data;boundary=" & Boundary)
Call web.setRequestHeader("Connection", "Keep-Alive")
Call web.setRequestHeader("cache-control", "no-cache")
Dim objStream, strData
Set objStream = CreateObject("ADODB.Stream")
objStream.Charset = "utf-8"
objStream.Open
objStream.LoadFromFile (par_filename)
strData = objStream.ReadText()
Dim getFileResult
getFileResult = GetFile(par_filename)
ls_param = vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""object_id""" & vbNewLine & vbNewLine & par_objectID & _
vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""description""" & vbNewLine & vbNewLine & par_description & _
vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""file""; filename=""" & par_filename & """" & vbNewLine & _
vbNewLine & strData & vbNewLine & vbNewLine & "--" & Boundary & "--" & vbNewLine
Call web.Send(ls_param)
end function
everything seems fine, but when I try to open the uploaded file, the pdf-reader tells me the file has a password. When I compare the files with notepad++ I can see that there is a difference. The "text part" seems to be identical but the "data"-part seems to have changed.
So this are the first few lines of the original:
%PDF-1.6
%âãÏÓ
37 0 obj <</Linearized 1/L 20597/O 40/E 14115/N 1/T 19795/H [ 1005 215]>>
endobj
and this is the file which was uploaded:
%PDF-1.6
%����
37 0 obj <</Linearized 1/L 20597/O 40/E 14115/N 1/T 19795/H [ 1005 215]>>
endobj
The second line is different. And the same happens with all of the content which is no text. Another example from a line in the center of the file:
Original:
s†fŸ«¸"$ ºƒŸ44}2šÔ#Y•¨×Ç,(ŒA-$ÈÇÝŠëâÓˆea‰,Òs<W²«äÒv{ r8¸ o*=ËîÁ—œ 5´xÎ&:‘Š‚2bÁnu:˜²ºú/nâ¼æ·ig–£‘±Åô3]E
file which was uploaded:
s�f���"$ ���44}2��#Y����,(�A-$��݊��ӈea�,�s<W����v{ r8� o*=����� 5�x�&:���2b�nu:���/n���ig������3]E
So: What im doing wrong? Something releated with the encoding I suppose.
With the help of user omegastripes and his hint to this example: File updload in post form in VBS I solved my problem.
The content of the file has to be read and sent to the host binary (not as a string as I did)
This code works for me:
Public Function sap_addTest(ByVal par_objectID As String, ByVal par_description As String, ByVal par_filename As String) As Integer
Dim ls_param As String
Dim text As String
Dim line As String
Dim url As String
Dim web As MSXML2.XMLHTTP60
url = "http://someurl.xml"
Set web = CreateObject("MSXML2.XMLHTTP")
Call web.Open("POST", url, False)
Const Boundary As String = "AaB03x"
Call web.setRequestHeader("content-type", "multipart/form-data;boundary=" & Boundary)
Call web.setRequestHeader("ws-callingapplication", sys_db)
Call web.setRequestHeader("Connection", "Keep-Alive")
Call web.setRequestHeader("cache-control", "no-cache")
Dim baBuffer() As Byte
Dim bytData
Dim bytPayLoad
With CreateObject("ADODB.Stream")
.Type = 1
.Mode = 3
.Open
.LoadFromFile par_filename
bytData = .Read
End With
With CreateObject("ADODB.Stream")
.Mode = 3
.Charset = "Windows-1252"
.Open
.Type = 2
.WriteText vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""object_id""" & vbNewLine & vbNewLine & par_objectID
.WriteText vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""description""" & vbNewLine & vbNewLine & par_description
.WriteText vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""file""; filename=""" & par_filename & """" & vbNewLine
.WriteText vbNewLine
.Position = 0
.Type = 1
.Position = .Size
.Write bytData
.Position = 0
.Type = 2
.Position = .Size
.WriteText vbNewLine & vbNewLine & "--" & Boundary & "--" & vbNewLine
.Position = 0
.Type = 1
bytPayLoad = .Read
End With
Call web.Send(bytPayLoad)
'Debug.Print web.status
'Debug.Print web.responseText
End Function