Search code examples
vbaexcelibm-connections

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


I am trying to post a forum topic with attached image file to IBM Connections 5.0 using Excel VBA. According to IBM Connections API description a multipart request will be required here.

What I already managed is to post a forum topic without attachment and also attaching a text or image file to an existing wiki page. Therefore I assume that the problem is not related with these aspects but rather with the correct formatting of the multipart request. API description is not very clear to me here and I tried several things I found about multipart requests in other help forums. But all I get is a response "400 bad request".

Maybe some of you experts can give me a hint about my code:

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

Solution

  • We found out what the problem was. It was indeed about the formatting of the multipart request. You need to be very careful with the CrLf characters ...

    Public Sub CreateForumPost()
        '...
    
        'create forum post
        sBody = vbCrLf & "--" & sBoundary & vbCrLf & vbCrLf
    
        '...
        sBody = sBody & sGetFile("c:\temp\dummy.txt") & vbCrLf
        sBody = sBody & "--" & sBoundary & "--"
    
        '...
    End Sub
    

    Now it works. Nevertheless many thanks for your support!