Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.0.0+

Jira Cloud: Download an Issue Attachment

See more Jira Examples

Find an attachment by issue key and filename, obtain its ID from issue metadata, and download it through the documented attachment-content endpoint. The code requires exactly one matching filename; if duplicates exist, select an attachment ID explicitly using its author or creation time.

The example asks Jira to return content without a redirect and handles an HTTPS redirect if one is returned. A separate Http object downloads a signed URL without forwarding the Jira credentials. The destination is an application-chosen local path, not an untrusted server filename. Create the output directory first. This simple version buffers the response in memory, so use a streaming download for very large attachments.

The code uses an Atlassian account email and an API token without scopes with the site URL. For a scoped API token, keep Basic authentication and change baseUrl to https://api.atlassian.com/ex/jira/YOUR_CLOUD_ID. For OAuth2, use that gateway URL, omit Login/Password/BasicAuth, and set http.AuthToken to the access token. See API token authentication and OAuth2 authorization. The account must have permission for the operation; OAuth scopes do not grant additional Jira permissions.

Targets Jira Cloud. Jira Data Center has a separate REST API and may require different paths, authentication, or payloads.

Atlassian REST API documentation

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    const wchar_t *baseUrl;
    HCkStringBuilderW url;
    HCkHttpResponseW resp;
    HCkJsonObjectW reply;
    const wchar_t *issueKey;
    const wchar_t *attachmentName;
    const wchar_t *attachmentId;
    const wchar_t *localPath;
    int matches;
    int i;
    HCkStringBuilderW location;
    HCkHttpW download;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    CkHttpW_putBasicAuth(http,TRUE);
    //  Read these credentials from private application configuration.
    CkHttpW_putLogin(http,L"YOUR_ATLASSIAN_EMAIL");
    CkHttpW_putPassword(http,L"YOUR_API_TOKEN");
    baseUrl = L"https://your-domain.atlassian.net";
    CkHttpW_putAccept(http,L"application/json");
    url = CkStringBuilderW_Create();
    resp = CkHttpResponseW_Create();
    reply = CkJsonObjectW_Create();
    issueKey = L"PROJECT-123";
    CkHttpW_SetUrlVar(http,L"issueKey",issueKey);
    attachmentName = L"report.pdf";
    attachmentId = L"";
    localPath = L"qa_output/report.pdf";
    matches = 0;
    CkStringBuilderW_SetString(url,baseUrl);
    CkStringBuilderW_Append(url,L"/rest/api/3/issue/{$issueKey}?fields=attachment");
    success = CkHttpW_HttpNoBody(http,L"GET",CkStringBuilderW_getAsString(url),resp);
    if (CkHttpResponseW_getStatusCode(resp) == 0) {
        wprintf(L"No HTTP response was received. Check connectivity and TLS settings.\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        return;
    }

    if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) < 200) || (CkHttpResponseW_getStatusCode(resp) >= 300)) {
        wprintf(L"Request failed. HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
        if (CkHttpResponseW_getStatusCode(resp) == 429) {
            wprintf(L"Rate limited. Retry-After: %s\n",CkHttpResponseW_getHeaderField(resp,L"Retry-After"));
        }

        //  Inspect this response locally; it can contain private Jira data.
        wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        return;
    }

    success = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
    if (success != TRUE) {
        wprintf(L"Expected a JSON object in the response.\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        return;
    }

    if (CkJsonObjectW_JsonTypeOf(reply,L"fields.attachment") != 4) {
        wprintf(L"Expected an array: fields.attachment\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        return;
    }

    i = 0;
    while ((i < CkJsonObjectW_SizeOfArray(reply,L"fields.attachment"))) {
        CkJsonObjectW_putI(reply,i);
        if (CkJsonObjectW_StringOfEquals(reply,L"fields.attachment[i].filename",attachmentName,TRUE) == TRUE) {
            attachmentId = CkJsonObjectW_stringOf(reply,L"fields.attachment[i].id");
            matches = matches + 1;
        }

        i = i + 1;
    }

    if ((matches != 1) || (attachmentId == L"")) {
        wprintf(L"Expected exactly one attachment with this filename. Matches: %d\n",matches);
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        return;
    }

    CkHttpW_SetUrlVar(http,L"attachmentId",attachmentId);
    CkHttpW_putFollowRedirects(http,FALSE);
    CkHttpW_putAccept(http,L"*/*");
    CkStringBuilderW_SetString(url,baseUrl);
    CkStringBuilderW_Append(url,L"/rest/api/3/attachment/content/{$attachmentId}?redirect=false");
    success = CkHttpW_HttpNoBody(http,L"GET",CkStringBuilderW_getAsString(url),resp);
    if ((CkHttpResponseW_getStatusCode(resp) == 302) || (CkHttpResponseW_getStatusCode(resp) == 303) || (CkHttpResponseW_getStatusCode(resp) == 307)) {
        location = CkStringBuilderW_Create();
        CkStringBuilderW_SetString(location,CkHttpResponseW_getHeaderField(resp,L"Location"));
        if (CkStringBuilderW_StartsWith(location,L"https://",FALSE) != TRUE) {
            wprintf(L"Expected an HTTPS attachment redirect.\n");
            CkHttpW_Dispose(http);
            CkStringBuilderW_Dispose(url);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(reply);
            CkStringBuilderW_Dispose(location);
            return;
        }

        download = CkHttpW_Create();
        CkHttpW_putRequireSslCertVerify(download,TRUE);
        //  The signed URL authorizes this request; do not add Jira credentials.
        success = CkHttpW_HttpNoBody(download,L"GET",CkStringBuilderW_getAsString(location),resp);
    }

    if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) != 200)) {
        wprintf(L"Attachment download failed. HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        CkStringBuilderW_Dispose(location);
        CkHttpW_Dispose(download);
        return;
    }

    success = CkHttpResponseW_SaveBodyBinary(resp,localPath);
    if (success != TRUE) {
        wprintf(L"Could not save the downloaded attachment.\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(reply);
        CkStringBuilderW_Dispose(location);
        CkHttpW_Dispose(download);
        return;
    }

    wprintf(L"Saved attachment to %s\n",localPath);


    CkHttpW_Dispose(http);
    CkStringBuilderW_Dispose(url);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(reply);
    CkStringBuilderW_Dispose(location);
    CkHttpW_Dispose(download);

    }