gpt4 book ai didi

VBA 通过 POST 方法将二进制代码的文件发送到 API

转载 作者:行者123 更新时间:2023-12-04 04:08:22 25 4
gpt4 key购买 nike

我的代码有最后一个问题。通过 POST 变量从 Outlook 发送代码到 API。

我的最后一个问题是如何在一个 POST 请求中向 API 发送变量和邮件附件。

邮件附件的第一个 7zip 压缩:

strSource = cstrFileAttachment & "*.*"
strTarget = cstrFileattachment & "Zip\attachment.zip"
strPassword = randomPassword(cintLongPassword)
strCommand = """" & PathZipProgram & """ a -tzip """ & strTarget & _
""" -p" & strPassword & " """ & strSource & """"

现在我有 c:\attachment\attachment.zip

下一部分是向 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

发送的是变量,但 fileAttachment 是作为字符串发送的,因此 API 获取文件保存的路径。

我的问题是如何将下面的代码(在互联网上找到)实现到我的代码 sendDataToApi 和 POST attachment.zip 作为字符串的二进制替代。

    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

更新:

当我添加来自@Tim Williams 的部分代码时

在我的数据库中,文件保存为 /tmp/phpAJOtVw 我做错了什么?

最佳答案

Upload 是一个独立的方法,因此您应该可以这样调用它:

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, "&", "|")

您应该从 Upload 的顶部删除这些硬编码值:

strUploadUrl = "https://url.domain.com/api/data"
strFilePath = cstrFilepathAttachment & "Zip\attachment.zip"

关于VBA 通过 POST 方法将二进制代码的文件发送到 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62165095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com