我有一个curl命令,可以将一些数据上传到API并返回结果
curl -X POST -H 'Content-Type: text/csv' --data-binary @data/data.csv https://some.url.com/invocations > data/churn_scored.jsonl
但是,在阅读文档(https://docs.microsoft.com/en-au/windows/desktop/WinHttp/winhttprequest)之后,我无法弄清楚如何使用VBA来完成此操作。
特别是,如果我可以将Excel中的一系列单元格视为CSV并将其上传,那就太好了。
另一篇文章(Equivalent cURL in VBA?)显示了如何卷曲端点,但没有显示如何使用@命令上传数据,这是此问题的重点。
答案 0 :(得分:1)
尝试
Public saveName as string
Function SaveSheetAsWorkbook()
Dim NewWb As Workbook
Dim ws As Worksheet
saveName = "c:\1.csv"
Set ws = wbT.Worksheets("Sheet1")
Set NewWb = Workbooks.Add
With NewWb
ws.Copy Before:=.Worksheets(1)
If .Worksheets.Count > 1 Then .Worksheets(2).Delete
.SaveAs saveName, xlCSV
.Close
End With
End Function
Function AuthSite()
Dim WinHttpReq, oStream
Set WinHttpReq = CreateObject("WINHTTP.WinHTTPRequest.5.1")
WinHttpReq.Open "POST", "https://some.url.com/invocations", False
WinHttpReq.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0"
WinHttpReq.setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
WinHttpReq.setRequestHeader "Accept-Language", "en-US;q=0.5"
'WinHttpReq.setRequestHeader "Accept-Encoding", "gzip, deflate, br"
WinHttpReq.setRequestHeader "Referer", strUrls
'WinHttpReq.setRequestHeader "Cookie", cookie
WinHttpReq.setRequestHeader "Connection", "keep-alive"
WinHttpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WinHttpReq.send saveName
Set AuthSite = WinHttpReq
End Function
或使用多部分形式
Public Boundary as string
Function UploadFile(ByVal filePath As String, ByVal FileName As String)
Dim sFormData As String
Randomize
Boundary = "---------------------------" & Int(999999999999999# * Rnd)
sFormData = GetFile(filePath & "\" & FileName)
D = "--" + Boundary + vbCrLf
'------1----------
strF = "_method"
strFval = "POST"
D = D + "Content-Disposition: form-data; name=""" + strF + """;" + vbCrLf + vbCrLf
D = D + strFval
D = D + vbCrLf + "--" + Boundary + vbCrLf
'Build source form with file contents
FieldName = "yourFieldName"
D = D + "Content-Disposition: form-data; name=""" + FieldName + """;"
D = D + " filename=""" + FileName + """" + vbCrLf
D = D + "Content-Type: application" + vbCrLf + vbCrLf
D = D + sFormData
D = D + vbCrLf + "--" + Boundary + "--" + vbCrLf
'Post the data To the destination URL
UploadFile = D
End Function
'read binary file As a string value
Function GetFile(ByVal FileName As String) As String
Dim FileContents() As Byte, FileNumber As Integer
ReDim FileContents(FileLen(FileName) - 1)
FileNumber = FreeFile
Open FileName For Binary As FileNumber
Get FileNumber, , FileContents
Close FileNumber
GetFile = StrConv(FileContents, vbUnicode)
End Function
Function AuthSite2()
Dim WinHttpReq, oStream
Set WinHttpReq = CreateObject("WINHTTP.WinHTTPRequest.5.1")
strPost = UploadFile(fPath, fName)
ReDim bFormData(Len(strPost) - 1)
bFormData = StrConv(strPost, vbFromUnicode)
WinHttpReq.Open "POST", "https://some.url.com/invocations", False
WinHttpReq.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0"
WinHttpReq.setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
WinHttpReq.setRequestHeader "Accept-Language", "en-US;q=0.5"
'WinHttpReq.setRequestHeader "Accept-Encoding", "gzip, deflate, br"
WinHttpReq.setRequestHeader "Referer", "https://some.url.com/invocations"
WinHttpReq.setRequestHeader "Connection", "keep-alive"
WinHttpReq.setRequestHeader "Content-Type", "multipart/form-data; boundary=" + Boundary + vbCrLf
WinHttpReq.send bFormData
Set AuthSite2 = WinHttpReq
End Function