Unicode C++ 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'

Unicode C++ Example

#include <CkRestW.h>
#include <CkStreamW.h>
#include <CkJsonArrayW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    CkRestW rest;
    bool success;

    //  URL: https://your-domain.atlassian.net/rest/api/2/issue/SCRUM-15/attachments
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect(L"your-domain.atlassian.net",port,bTls,bAutoReconnect);
    if (success != true) {
        wprintf(L"ConnectFailReason: %d\n",rest.get_ConnectFailReason());
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    rest.SetAuthBasic(L"jira@example.com",L"JIRA_API_TOKEN");

    rest.put_PartSelector(L"1");
    CkStreamW fileStream1;
    fileStream1.put_SourceFile(L"starfish.jpg");
    rest.AddHeader(L"Content-Disposition",L"form-data; name=\"file\"; filename=\"starfish.jpg\"");
    rest.AddHeader(L"Content-Type",L"image/jpeg");
    rest.SetMultipartBodyStream(fileStream1);

    rest.put_PartSelector(L"2");
    CkStreamW fileStream2;
    fileStream2.put_SourceFile(L"sample2.docx");
    rest.AddHeader(L"Content-Disposition",L"form-data; name=\"file\"; filename=\"sample2.docx\"");
    rest.AddHeader(L"Content-Type",L"application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    rest.SetMultipartBodyStream(fileStream2);

    rest.put_PartSelector(L"3");
    CkStreamW fileStream3;
    fileStream3.put_SourceFile(L"sample.pdf");
    rest.AddHeader(L"Content-Disposition",L"form-data; name=\"file\"; filename=\"sample.pdf\"");
    rest.AddHeader(L"Content-Type",L"application/pdf");
    rest.SetMultipartBodyStream(fileStream3);

    rest.put_PartSelector(L"0");

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

    const wchar_t *strResponseBody = rest.fullRequestMultipart(L"POST",L"/rest/api/2/issue/SCRUM-15/attachments");
    if (rest.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();
    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",rest.responseHeader());
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",strResponseBody);
        return;
    }

    CkJsonArrayW jsonResponse;
    jsonResponse.Load(strResponseBody);

    int arrIdx = 0;
    int numArrayObjects = jsonResponse.get_Size();
    //  Iterate over the members of the array.
    while (arrIdx < numArrayObjects) {
        //  Make sure the array member at this index is a JSON object.
        if (jsonResponse.TypeAt(arrIdx) == 3) {
            CkJsonObjectW *jObj = jsonResponse.ObjectAt(arrIdx);

            //  ...

            delete jObj;
        }

        arrIdx = arrIdx + 1;
    }
    }

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