PowerBuilder Jira - Issues: Add (Upload) Attachments to an Issue

Back to Index

Add one or more attachments to an issue. This example will add 3 attachments to the issue "SCRUM-15".

Documentation: https://developers.atlassian.com/cloud/jira/platform/rest/#api-api-2-issue-issueIdOrKey-attachments-post

CURL Command

curl -X POST --user jira@example.com:JIRA_API_TOKEN \
  --header 'Accept: application/json' \
  --header 'Content-Type: multipart/form-data' \
  --header 'X-Atlassian-Token: no-check' \
  --form 'name=file; file=@starfish.jpg' \
  --form 'name=file; file=@sample2.docx' \
  --form 'name=file; file=@sample.pdf' \
  --url 'https://your-domain.atlassian.net/rest/api/2/issue/SCRUM-15/attachments'

PowerBuilder Example

integer li_rc
oleobject loo_Rest
integer li_Success
integer li_BTls
integer li_Port
integer li_BAutoReconnect
oleobject loo_FileStream1
oleobject loo_FileStream2
oleobject loo_FileStream3
string ls_StrResponseBody
integer li_RespStatusCode
oleobject loo_JsonResponse
integer li_ArrIdx
integer li_NumArrayObjects
oleobject loo_JObj

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat_9_5_0.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  URL: https://your-domain.atlassian.net/rest/api/2/issue/SCRUM-15/attachments
li_BTls = 1
li_Port = 443
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("your-domain.atlassian.net",li_Port,li_BTls,li_BAutoReconnect)
if li_Success <> 1 then
    Write-Debug "ConnectFailReason: " + string(loo_Rest.ConnectFailReason)
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

loo_Rest.SetAuthBasic("jira@example.com","JIRA_API_TOKEN")

loo_Rest.PartSelector = "1"
loo_FileStream1 = create oleobject
li_rc = loo_FileStream1.ConnectToNewObject("Chilkat_9_5_0.Stream")

loo_FileStream1.SourceFile = "starfish.jpg"
loo_Rest.AddHeader("Content-Disposition","form-data; name=\"file\"; filename=\"starfish.jpg\"")
loo_Rest.AddHeader("Content-Type","image/jpeg")
loo_Rest.SetMultipartBodyStream(loo_FileStream1)

loo_Rest.PartSelector = "2"
loo_FileStream2 = create oleobject
li_rc = loo_FileStream2.ConnectToNewObject("Chilkat_9_5_0.Stream")

loo_FileStream2.SourceFile = "sample2.docx"
loo_Rest.AddHeader("Content-Disposition","form-data; name=\"file\"; filename=\"sample2.docx\"")
loo_Rest.AddHeader("Content-Type","application/vnd.openxmlformats-officedocument.wordprocessingml.document")
loo_Rest.SetMultipartBodyStream(loo_FileStream2)

loo_Rest.PartSelector = "3"
loo_FileStream3 = create oleobject
li_rc = loo_FileStream3.ConnectToNewObject("Chilkat_9_5_0.Stream")

loo_FileStream3.SourceFile = "sample.pdf"
loo_Rest.AddHeader("Content-Disposition","form-data; name=\"file\"; filename=\"sample.pdf\"")
loo_Rest.AddHeader("Content-Type","application/pdf")
loo_Rest.SetMultipartBodyStream(loo_FileStream3)

loo_Rest.PartSelector = "0"

loo_Rest.AddHeader("Content-Type","multipart/form-data")
loo_Rest.AddHeader("X-Atlassian-Token","no-check")
loo_Rest.AddHeader("Accept","application/json")

ls_StrResponseBody = loo_Rest.FullRequestMultipart("POST","/rest/api/2/issue/SCRUM-15/attachments")
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_FileStream1
    destroy loo_FileStream2
    destroy loo_FileStream3
    return
end if

li_RespStatusCode = loo_Rest.ResponseStatusCode
if li_RespStatusCode >= 400 then
    Write-Debug "Response Status Code = " + string(li_RespStatusCode)
    Write-Debug "Response Header:"
    Write-Debug loo_Rest.ResponseHeader
    Write-Debug "Response Body:"
    Write-Debug ls_StrResponseBody
    destroy loo_Rest
    destroy loo_FileStream1
    destroy loo_FileStream2
    destroy loo_FileStream3
    return
end if

loo_JsonResponse = create oleobject
li_rc = loo_JsonResponse.ConnectToNewObject("Chilkat_9_5_0.JsonArray")

loo_JsonResponse.Load(ls_StrResponseBody)

li_ArrIdx = 0
li_NumArrayObjects = loo_JsonResponse.Size
//  Iterate over the members of the array.
do while li_ArrIdx < li_NumArrayObjects
    //  Make sure the array member at this index is a JSON object.
    if loo_JsonResponse.TypeAt(li_ArrIdx) = 3 then
        loo_JObj = loo_JsonResponse.ObjectAt(li_ArrIdx)

        //  ...

        destroy loo_JObj
    end if

    li_ArrIdx = li_ArrIdx + 1
loop


destroy loo_Rest
destroy loo_FileStream1
destroy loo_FileStream2
destroy loo_FileStream3
destroy loo_JsonResponse

Sample JSON Response Body

[
  {
    "self": "https://chilkat.atlassian.net/rest/api/2/attachment/10001",
    "id": "10001",
    "filename": "starfish.jpg",
    "author": {
      "self": "https://chilkat.atlassian.net/rest/api/2/user?username=admin",
      "name": "admin",
      "key": "admin",
      "accountId": "557058:be8b47b5-3bc0-43f6-b6b2-2cca0de12204",
      "emailAddress": "admin@@chilkatsoft.com",
      "avatarUrls": {
        "48x48": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue",
        "24x24": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue",
        "16x16": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue",
        "32x32": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue"
      },
      "displayName": "Chilkat Admin",
      "active": true,
      "timeZone": "America/Chicago"
    },
    "created": "2018-04-13T10:04:44.966-0500",
    "size": 6229,
    "mimeType": "image/jpeg",
    "content": "https://chilkat.atlassian.net/secure/attachment/10001/starfish.jpg",
    "thumbnail": "https://chilkat.atlassian.net/secure/thumbnail/10001/starfish.jpg"
  },
  {
    "self": "https://chilkat.atlassian.net/rest/api/2/attachment/10002",
    "id": "10002",
    "filename": "sample2.docx",
    "author": {
      "self": "https://chilkat.atlassian.net/rest/api/2/user?username=admin",
      "name": "admin",
      "key": "admin",
      "accountId": "557058:be8b47b5-3bc0-43f6-b6b2-2cca0de12204",
      "emailAddress": "admin@@chilkatsoft.com",
      "avatarUrls": {
        "48x48": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue",
        "24x24": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue",
        "16x16": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue",
        "32x32": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue"
      },
      "displayName": "Chilkat Admin",
      "active": true,
      "timeZone": "America/Chicago"
    },
    "created": "2018-04-13T10:04:45.706-0500",
    "size": 21082,
    "mimeType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "content": "https://chilkat.atlassian.net/secure/attachment/10002/sample2.docx"
  },
  {
    "self": "https://chilkat.atlassian.net/rest/api/2/attachment/10003",
    "id": "10003",
    "filename": "sample.pdf",
    "author": {
      "self": "https://chilkat.atlassian.net/rest/api/2/user?username=admin",
      "name": "admin",
      "key": "admin",
      "accountId": "557058:be8b47b5-3bc0-43f6-b6b2-2cca0de12204",
      "emailAddress": "admin@@chilkatsoft.com",
      "avatarUrls": {
        "48x48": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue",
        "24x24": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue",
        "16x16": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue",
        "32x32": "https://avatar-cdn.atlassian.com/16d54dcc6d4bef86fd7ee62a7cf6334a?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F16d54dcc6d4bef86fd7ee62a7cf6334a%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue"
      },
      "displayName": "Chilkat Admin",
      "active": true,
      "timeZone": "America/Chicago"
    },
    "created": "2018-04-13T10:04:46.292-0500",
    "size": 178399,
    "mimeType": "application/pdf",
    "content": "https://chilkat.atlassian.net/secure/attachment/10003/sample.pdf"
  }
]