2

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
SteffenB
  • 80
  • 6
  • Have you tried it exactly the way which is described in the API description? So not having the boundary into the `Content-Type` header but instead using the fixed `--MIME_boundary` boundary and not having double `CRLF` behind the `Content-Disposition` and not having an `--MIME_boundary--` at the end? This is not accordingly the RFC but maybe the API is expecting it exactly this way. – Axel Richter Jul 17 '15 at 07:57
  • can you capture a trace using fiddler ? I'm interested in the response content – Paul Bastide Jul 17 '15 at 15:22
  • @AxelRichter Thanks for your comment. I tried this but without success. – SteffenB Jul 20 '15 at 06:25
  • @PaulBastide Thanks for pointing me to fiddler. I installed it and here is the trace of the response (hope this is what you meant): – SteffenB Jul 20 '15 at 06:26
  • Invalid Entry means you are sending invalid content to the server. can you paste the entry that's in the request here? it seems that you aren't sending the right data to the server – Paul Bastide Jul 20 '15 at 11:20
  • @PaulBastide Here you go ... – SteffenB Jul 20 '15 at 12:58
  • if you simply do the POST of the atom entry with Content-Type: application/atom+xml does that work? is it strictly the multipart? I've never used multipart with forums – Paul Bastide Jul 22 '15 at 12:03
  • @PaulBastide Yes if I only post the content without the attachment and without using the multipart request, then the forum post is created successfully. What I can see from comparing request and response is, that I try to send 685 bytes but only 196 are received. But I cannot match this to the telegram and find out where the problem is. – SteffenB Jul 22 '15 at 13:02
  • Is there a way to post the forum topic first and attach the file later in a second request? If yes, how can I do it? – SteffenB Jul 22 '15 at 13:09
  • we don't disclose that in the documentation. I'll look to see if we can add that. – Paul Bastide Jul 22 '15 at 13:52

1 Answers1

0

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!

SteffenB
  • 80
  • 6