如何使用 Excel VBA post 带有 IBM Connections 附件的论坛主题

How to post a forum topic with attachment to IBM Connections using Excel VBA

我正在尝试 post 使用 Excel VBA 将带有附加图像文件的论坛主题添加到 IBM Connections 5.0。 根据 IBM Connections API description 此处需要多部分请求。

我已经做到的是 post 一个没有附件的论坛主题,并将文本或图像文件附加到现有的 wiki 页面。因此,我假设问题与这些方面无关,而是与多部分请求的正确格式有关。 API 这里的描述对我来说不是很清楚,我尝试了一些在其他帮助论坛中发现的关于多部分请求的事情。但我得到的只是一个响应“400 错误请求”。

也许你们中的一些专家可以给我一些关于我的代码的提示:

Public Sub CreateForumPost()
    Const sBoundary As String = "2588eb82-2e1c-4aec-9f4f-d65a3ecf8fab"
    Dim oHttp As MSXML2.xmlhttp
    Dim sUrl As String
    Dim sBody As String

    'create XMLHTTP object and URL
    Set oHttp = CreateObject("MSXML2.XMLHTTP")
    sUrl = "https://my-connect-server/forums/atom/topics?forumUuid=9e51cbfb-4b1d-405d-9835-dbd087c49a65"

    'create forum post
    sBody = "--" & sBoundary & vbCrLf
    sBody = sBody & "<?xml version=""1.0"" encoding=""UTF-8""?>"
    sBody = sBody & "<entry xmlns=""http://www.w3.org/2005/Atom"" xmlns:app=""http://www.w3.org/2007/app"" xmlns:snx=""http://www.ibm.com/xmlns/prod/sn"">"
    sBody = sBody & "<category scheme=""http://www.ibm.com/xmlns/prod/sn/type"" term=""forum-topic""/>"
    sBody = sBody & "<title type=""text""> " & "My Title" & " </title>"
    sBody = sBody & "<category term=""question"" scheme=""http://www.ibm.com/xmlns/prod/sn/flags""/>"
    sBody = sBody & "<category term=""" & "my-tag" & """/>"
    sBody = sBody & "<content type=""html""> " & "My post content" & " </content>"
    sBody = sBody & "</entry>" & vbCrLf

    sBody = sBody & "--" & sBoundary & vbCrLf
    sBody = sBody & "Content-Disposition: attachment; filename=""dummy.txt""" & vbCrLf & vbCrLf
    sBody = sBody & sGetFile("c:\temp\dummy.txt") & vbCrLf
    sBody = sBody & "--" & sBoundary & "--" & vbCrLf

    Call oHttp.Open("POST", sUrl, False)
    Call oHttp.setRequestHeader("Content-Type", "multipart/related;boundary=" & sBoundary & ";type=""application/atom+xml""")
    Call oHttp.send(pvToByteArray(sBody))

    If oHttp.Status = 201 Then
        Call MsgBox("success")
    Else
        Call MsgBox("error")
        Stop
    End If
End Sub

Private Function sGetFile(sName As String) As String
    Dim abyContent() As Byte
    Dim iNumber As Integer
    Dim lLen As Long

    lLen = FileLen(sName)
    If lLen > 0 Then
        ReDim abyContent(lLen - 1)
        iNumber = FreeFile
        Open sName For Binary Access Read As iNumber
          Get iNumber, , abyContent
        Close iNumber
        sGetFile = StrConv(abyContent, vbUnicode)
    Else
        sGetFile = ""
    End If
End Function

Function pvToByteArray(sText As String) As Byte()
    pvToByteArray = StrConv(sText, vbFromUnicode)
End Function

我们发现了问题所在。这确实与多部分请求的格式有关。您需要非常小心 CrLf 字符 ...

Public Sub CreateForumPost()
    '...

    'create forum post
    sBody = vbCrLf & "--" & sBoundary & vbCrLf & vbCrLf

    '...
    sBody = sBody & sGetFile("c:\temp\dummy.txt") & vbCrLf
    sBody = sBody & "--" & sBoundary & "--"

    '...
End Sub

现在可以了。不过还是非常感谢大家的支持!