Visual FoxPro 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'

Visual FoxPro Example

LOCAL loRest
LOCAL lnSuccess
LOCAL lnBTls
LOCAL lnPort
LOCAL lnBAutoReconnect
LOCAL loFileStream1
LOCAL loFileStream2
LOCAL loFileStream3
LOCAL lcStrResponseBody
LOCAL lnRespStatusCode
LOCAL loJsonResponse
LOCAL lnArrIdx
LOCAL lnNumArrayObjects
LOCAL loJObj

loRest = CreateObject('Chilkat_9_5_0.Rest')

*  URL: https://your-domain.atlassian.net/rest/api/2/issue/SCRUM-15/attachments
lnBTls = 1
lnPort = 443
lnBAutoReconnect = 1
lnSuccess = loRest.Connect("your-domain.atlassian.net",lnPort,lnBTls,lnBAutoReconnect)
IF (lnSuccess <> 1) THEN
    ? "ConnectFailReason: " + STR(loRest.ConnectFailReason)
    ? loRest.LastErrorText
    RELEASE loRest
    CANCEL
ENDIF

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

loRest.PartSelector = "1"
loFileStream1 = CreateObject('Chilkat_9_5_0.Stream')
loFileStream1.SourceFile = "starfish.jpg"
loRest.AddHeader("Content-Disposition",'form-data; name="file"; filename="starfish.jpg"')
loRest.AddHeader("Content-Type","image/jpeg")
loRest.SetMultipartBodyStream(loFileStream1)

loRest.PartSelector = "2"
loFileStream2 = CreateObject('Chilkat_9_5_0.Stream')
loFileStream2.SourceFile = "sample2.docx"
loRest.AddHeader("Content-Disposition",'form-data; name="file"; filename="sample2.docx"')
loRest.AddHeader("Content-Type","application/vnd.openxmlformats-officedocument.wordprocessingml.document")
loRest.SetMultipartBodyStream(loFileStream2)

loRest.PartSelector = "3"
loFileStream3 = CreateObject('Chilkat_9_5_0.Stream')
loFileStream3.SourceFile = "sample.pdf"
loRest.AddHeader("Content-Disposition",'form-data; name="file"; filename="sample.pdf"')
loRest.AddHeader("Content-Type","application/pdf")
loRest.SetMultipartBodyStream(loFileStream3)

loRest.PartSelector = "0"

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

lcStrResponseBody = loRest.FullRequestMultipart("POST","/rest/api/2/issue/SCRUM-15/attachments")
IF (loRest.LastMethodSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loRest
    RELEASE loFileStream1
    RELEASE loFileStream2
    RELEASE loFileStream3
    CANCEL
ENDIF

lnRespStatusCode = loRest.ResponseStatusCode
IF (lnRespStatusCode >= 400) THEN
    ? "Response Status Code = " + STR(lnRespStatusCode)
    ? "Response Header:"
    ? loRest.ResponseHeader
    ? "Response Body:"
    ? lcStrResponseBody
    RELEASE loRest
    RELEASE loFileStream1
    RELEASE loFileStream2
    RELEASE loFileStream3
    CANCEL
ENDIF

loJsonResponse = CreateObject('Chilkat_9_5_0.JsonArray')
loJsonResponse.Load(lcStrResponseBody)

lnArrIdx = 0
lnNumArrayObjects = loJsonResponse.Size
*  Iterate over the members of the array.
DO WHILE lnArrIdx < lnNumArrayObjects
    *  Make sure the array member at this index is a JSON object.
    IF (loJsonResponse.TypeAt(lnArrIdx) = 3) THEN
        loJObj = loJsonResponse.ObjectAt(lnArrIdx)

        *  ...

        RELEASE loJObj
    ENDIF

    lnArrIdx = lnArrIdx + 1
ENDDO

RELEASE loRest
RELEASE loFileStream1
RELEASE loFileStream2
RELEASE loFileStream3
RELEASE loJsonResponse

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