如何将@data.file curl 命令转换为 VBA?

Posted

技术标签:

【中文标题】如何将@data.file curl 命令转换为 VBA?【英文标题】:How to convert @data.file curl command to VBA? 【发布时间】:2019-06-25 05:15:34 【问题描述】:

我有一个 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?) 展示了如何卷曲端点,但没有展示如何使用 @ 命令上传数据,这是这个问题的中心点。

【问题讨论】:

Equivalent cURL in VBA?的可能重复 【参考方案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

【讨论】:

似乎不起作用。我还需要设置Referer吗? strUrls 未定义。那是为了什么? 我得到的错误基本上是数据没有正确上传 标题不应该是 'Content-Type': 'text/csv' 吗?

以上是关于如何将@data.file curl 命令转换为 VBA?的主要内容,如果未能解决你的问题,请参考以下文章

如何将此 curl 命令转换为 Ruby rest-client put 请求?

将 curl 命令转换为 http 请求接口

将命令 cURL 转换为 HTTP 请求

如何将此 curl 请求转换为 Postman 请求

将 Curl 命令转换为在 Zapier Webhook 中运行

如何将此curl语句转换为python请求?