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

Marie B
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 13, 2018

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 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

 

2 answers

0 votes
Luis Serafini
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 5, 2022

Pudiste resolver , tengo mismo problema.

0 votes
Lachlan G
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 13, 2018

.pdf, .png, .jpg should all work. I'm not sure exactly what is going on but I'd guess it has something to do with how you're encoding the files.

 

Refer to the API docs here.https://docs.atlassian.com/software/jira/docs/api/REST/7.12.0/#api/2/attachment-getAttachment

 

- Lachlan Goodhew-Cook

Jira Service Desk Server

Suggest an answer

Log in or Sign up to answer