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

PureBasic Example

IncludeFile "CkStream.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkJsonArray.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ;  URL: https://your-domain.atlassian.net/rest/api/2/issue/SCRUM-15/attachments
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"your-domain.atlassian.net",port,bTls,bAutoReconnect)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkRest::ckSetAuthBasic(rest,"jira@example.com","JIRA_API_TOKEN")

    CkRest::setCkPartSelector(rest, "1")
    fileStream1.i = CkStream::ckCreate()
    If fileStream1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStream::setCkSourceFile(fileStream1, "starfish.jpg")
    CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "file" + Chr(34) + "; filename=" + Chr(34) + "starfish.jpg" + Chr(34))
    CkRest::ckAddHeader(rest,"Content-Type","image/jpeg")
    CkRest::ckSetMultipartBodyStream(rest,fileStream1)

    CkRest::setCkPartSelector(rest, "2")
    fileStream2.i = CkStream::ckCreate()
    If fileStream2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStream::setCkSourceFile(fileStream2, "sample2.docx")
    CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "file" + Chr(34) + "; filename=" + Chr(34) + "sample2.docx" + Chr(34))
    CkRest::ckAddHeader(rest,"Content-Type","application/vnd.openxmlformats-officedocument.wordprocessingml.document")
    CkRest::ckSetMultipartBodyStream(rest,fileStream2)

    CkRest::setCkPartSelector(rest, "3")
    fileStream3.i = CkStream::ckCreate()
    If fileStream3.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStream::setCkSourceFile(fileStream3, "sample.pdf")
    CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "file" + Chr(34) + "; filename=" + Chr(34) + "sample.pdf" + Chr(34))
    CkRest::ckAddHeader(rest,"Content-Type","application/pdf")
    CkRest::ckSetMultipartBodyStream(rest,fileStream3)

    CkRest::setCkPartSelector(rest, "0")

    CkRest::ckAddHeader(rest,"Content-Type","multipart/form-data")
    CkRest::ckAddHeader(rest,"X-Atlassian-Token","no-check")
    CkRest::ckAddHeader(rest,"Accept","application/json")

    strResponseBody.s = CkRest::ckFullRequestMultipart(rest,"POST","/rest/api/2/issue/SCRUM-15/attachments")
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkStream::ckDispose(fileStream1)
        CkStream::ckDispose(fileStream2)
        CkStream::ckDispose(fileStream3)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkRest::ckResponseStatusCode(rest)
    If respStatusCode >= 400
        Debug "Response Status Code = " + Str(respStatusCode)
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug strResponseBody
        CkRest::ckDispose(rest)
        CkStream::ckDispose(fileStream1)
        CkStream::ckDispose(fileStream2)
        CkStream::ckDispose(fileStream3)
        ProcedureReturn
    EndIf

    jsonResponse.i = CkJsonArray::ckCreate()
    If jsonResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckLoad(jsonResponse,strResponseBody)

    arrIdx.i = 0
    numArrayObjects.i = CkJsonArray::ckSize(jsonResponse)
    ;  Iterate over the members of the array.
    While arrIdx < numArrayObjects
        ;  Make sure the array member at this index is a JSON object.
        If CkJsonArray::ckTypeAt(jsonResponse,arrIdx) = 3
            jObj.i = CkJsonArray::ckObjectAt(jsonResponse,arrIdx)

            ;  ...

            CkJsonObject::ckDispose(jObj)

        EndIf

        arrIdx = arrIdx + 1
    Wend


    CkRest::ckDispose(rest)
    CkStream::ckDispose(fileStream1)
    CkStream::ckDispose(fileStream2)
    CkStream::ckDispose(fileStream3)
    CkJsonArray::ckDispose(jsonResponse)


    ProcedureReturn
EndProcedure

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"
  }
]