Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    const char *baseUrl;
    HCkStringBuilder url;
    HCkHttpResponse resp;
    HCkJsonObject reply;
    const char *issueKey;
    const char *attachmentName;
    const char *attachmentId;
    const char *localPath;
    int matches;
    int i;
    HCkStringBuilder location;
    HCkHttp download;

    success = FALSE;

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

    http = CkHttp_Create();
    CkHttp_putRequireSslCertVerify(http,TRUE);
    CkHttp_putBasicAuth(http,TRUE);
    //  Read these credentials from private application configuration.
    CkHttp_putLogin(http,"YOUR_ATLASSIAN_EMAIL");
    CkHttp_putPassword(http,"YOUR_API_TOKEN");
    baseUrl = "https://your-domain.atlassian.net";
    CkHttp_putAccept(http,"application/json");
    url = CkStringBuilder_Create();
    resp = CkHttpResponse_Create();
    reply = CkJsonObject_Create();
    issueKey = "PROJECT-123";
    CkHttp_SetUrlVar(http,"issueKey",issueKey);
    attachmentName = "report.pdf";
    attachmentId = "";
    localPath = "qa_output/report.pdf";
    matches = 0;
    CkStringBuilder_SetString(url,baseUrl);
    CkStringBuilder_Append(url,"/rest/api/3/issue/{$issueKey}?fields=attachment");
    success = CkHttp_HttpNoBody(http,"GET",CkStringBuilder_getAsString(url),resp);
    if (CkHttpResponse_getStatusCode(resp) == 0) {
        printf("No HTTP response was received. Check connectivity and TLS settings.\n");
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        return;
    }

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

        //  Inspect this response locally; it can contain private Jira data.
        printf("%s\n",CkHttpResponse_bodyStr(resp));
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        return;
    }

    success = CkJsonObject_Load(reply,CkHttpResponse_bodyStr(resp));
    if (success != TRUE) {
        printf("Expected a JSON object in the response.\n");
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        return;
    }

    if (CkJsonObject_JsonTypeOf(reply,"fields.attachment") != 4) {
        printf("Expected an array: fields.attachment\n");
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        return;
    }

    i = 0;
    while ((i < CkJsonObject_SizeOfArray(reply,"fields.attachment"))) {
        CkJsonObject_putI(reply,i);
        if (CkJsonObject_StringOfEquals(reply,"fields.attachment[i].filename",attachmentName,TRUE) == TRUE) {
            attachmentId = CkJsonObject_stringOf(reply,"fields.attachment[i].id");
            matches = matches + 1;
        }

        i = i + 1;
    }

    if ((matches != 1) || (attachmentId == "")) {
        printf("Expected exactly one attachment with this filename. Matches: %d\n",matches);
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        return;
    }

    CkHttp_SetUrlVar(http,"attachmentId",attachmentId);
    CkHttp_putFollowRedirects(http,FALSE);
    CkHttp_putAccept(http,"*/*");
    CkStringBuilder_SetString(url,baseUrl);
    CkStringBuilder_Append(url,"/rest/api/3/attachment/content/{$attachmentId}?redirect=false");
    success = CkHttp_HttpNoBody(http,"GET",CkStringBuilder_getAsString(url),resp);
    if ((CkHttpResponse_getStatusCode(resp) == 302) || (CkHttpResponse_getStatusCode(resp) == 303) || (CkHttpResponse_getStatusCode(resp) == 307)) {
        location = CkStringBuilder_Create();
        CkStringBuilder_SetString(location,CkHttpResponse_getHeaderField(resp,"Location"));
        if (CkStringBuilder_StartsWith(location,"https://",FALSE) != TRUE) {
            printf("Expected an HTTPS attachment redirect.\n");
            CkHttp_Dispose(http);
            CkStringBuilder_Dispose(url);
            CkHttpResponse_Dispose(resp);
            CkJsonObject_Dispose(reply);
            CkStringBuilder_Dispose(location);
            return;
        }

        download = CkHttp_Create();
        CkHttp_putRequireSslCertVerify(download,TRUE);
        //  The signed URL authorizes this request; do not add Jira credentials.
        success = CkHttp_HttpNoBody(download,"GET",CkStringBuilder_getAsString(location),resp);
    }

    if ((success != TRUE) || (CkHttpResponse_getStatusCode(resp) != 200)) {
        printf("Attachment download failed. HTTP %d\n",CkHttpResponse_getStatusCode(resp));
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        CkStringBuilder_Dispose(location);
        CkHttp_Dispose(download);
        return;
    }

    success = CkHttpResponse_SaveBodyBinary(resp,localPath);
    if (success != TRUE) {
        printf("Could not save the downloaded attachment.\n");
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        CkStringBuilder_Dispose(location);
        CkHttp_Dispose(download);
        return;
    }

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


    CkHttp_Dispose(http);
    CkStringBuilder_Dispose(url);
    CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(reply);
    CkStringBuilder_Dispose(location);
    CkHttp_Dispose(download);

    }