How to POST attachments to issues via REST API in vbscript.

Marie B March 13, 2018

Hello,

I try to post via REST API several types of files to an existing issue in JIRA (.docx, .pdf, .png, .jpg) but the only type that really works is file type .txt

All other file types are loaded in jira but I can't open them (like wrong format).

Can anyone help me with this - I'm just starting with vbscript...

This is the vbscript code I used:

Sub Test2_JiraRestApiPostAttachments()

  Dim oJiraService   Dim STR_BOUNDARY   Dim sRest   Dim sIssueNumber   Dim sFileDataStr, sPath , sStatus   Dim test_other_funtion

  sUsername = "something"   sPassword = "something"

 

  STR_BOUNDARY = "abc123-xyz123"   sEncbase64Auth = Base64Encode(sUsername & ":" & sPassword)

        Set oJiraService = CreateObject("MSXML2.XMLHTTP")         sIssueNumber = "TCELL-1318"

        'sPath = "r:\test2.docx"         sPath = "r:\ALM-snapshot.jpg"         testje =  Mid(sPath, InStrRev(sPath, "\") + 1)

        sFileDataStr =  "--" & STR_BOUNDARY &  vbCrLf & _               "Content-Disposition: form-data; name=""file"";Filename= """ & Mid(sPath, InStrRev(sPath, "\") + 1) & """" & vbCrLf & _               "Content-Type: application/octet-stream" &  vbCrLf & _               vbCrLf & GetFileBytes(sPath) & vbCrLf & "--" & _               STR_BOUNDARY & "--"

        With oJiraService

            .Open "POST", "http://www.jirapp.etc.../ep-jira/rest/api/latest/issue/" & sIssueNumber & "/attachments", False             .setRequestHeader "X-Atlassian-Token", "nocheck"             .setRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY             .setRequestHeader "Authorization", "Basic " & sEncbase64Auth             .send stringToByteArray(sFileDataStr)             sRest = .responseText             sStatus = .Status & " | " & .statusText

        End With

msgbox "Status : " & sStatus

    Set oJiraService = Nothing

end sub

 Function GetFileBytes(fPath )

    Dim binaryStream     Dim rs     Const adLongVarChar = 201     Dim readByteArray

    'Create Stream object     set binaryStream = CreateObject("ADODB.Stream")     binaryStream.Type = 1 'binary     binaryStream.Open

    binaryStream.LoadFromFile fpath     readByteArray = binaryStream.read     binaryStream.close

    'Convert the byte array data into a string and send it to JiraRestApiPostAttachment     Set rs = CreateObject("ADODB.Recordset")     rs.Fields.Append "temp", adLongVarChar, LenB(readByteArray)     rs.Open     rs.AddNew     rs("temp").AppendChunk readByteArray     rs.Update     GetFileBytes = rs("temp")     rs.Close     Set rs = Nothing     Set binaryStream = Nothing

End Function

 Function stringToByteArray(srcTxt)     'Convert a string to Byte Array in selected Charset

    Const adTypeBinary = 1     Dim bin

    Set bin = CreateObject("ADODB.Stream")     bin.Mode = 3 'adModeReadWrite     bin.Type = 2 'text     bin.charset = "ascii"     bin.Open     bin.writeText srcTxt     'rewind the stream and read bytes     bin.Position =0     bin.type = 1     stringToByteArray = bin.Read     bin.close     Set bin = Nothing

End Function

Function Base64Encode(inData)   'rfc1521   '2001 Antonin Foller, Motobit Software, http://Motobit.cz   Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"   Dim cOut, sOut, I

  'For each group of 3 bytes   For I = 1 To Len(inData) Step 3     Dim nGroup, pOut, sGroup

    'Create one long from this 3 bytes.     nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _       &H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))

    'Oct splits the long To 8 groups with 3 bits     nGroup = Oct(nGroup)

    'Add leading zeros     nGroup = String(8 - Len(nGroup), "0") & nGroup

    'Convert To base64     pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _       Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _       Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _       Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)

    'Add the part To OutPut string     sOut = sOut + pOut

    'Add a new line For Each 76 chars In dest (76*3/4 = 57)     'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf   Next   Select Case Len(inData) Mod 3     Case 1: '8 bit final       sOut = Left(sOut, Len(sOut) - 2) + "=="     Case 2: '16 bit final       sOut = Left(sOut, Len(sOut) - 1) + "="   End Select   Base64Encode = sOut End Function

 

0 comments

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events