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

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

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

    CkHttp::setCkRequireSslCertVerify(http, 1)
    CkHttp::setCkBasicAuth(http, 1)
    ;  Read these credentials from private application configuration.
    CkHttp::setCkLogin(http, "YOUR_ATLASSIAN_EMAIL")
    CkHttp::setCkPassword(http, "YOUR_API_TOKEN")
    baseUrl.s = "https://your-domain.atlassian.net"
    CkHttp::setCkAccept(http, "application/json")
    url.i = CkStringBuilder::ckCreate()
    If url.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

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

    issueKey.s = "PROJECT-123"
    CkHttp::ckSetUrlVar(http,"issueKey",issueKey)
    attachmentName.s = "report.pdf"
    attachmentId.s = ""
    localPath.s = "qa_output/report.pdf"
    matches.i = 0
    CkStringBuilder::ckSetString(url,baseUrl)
    CkStringBuilder::ckAppend(url,"/rest/api/3/issue/{$issueKey}?fields=attachment")
    success = CkHttp::ckHttpNoBody(http,"GET",CkStringBuilder::ckGetAsString(url),resp)
    If CkHttpResponse::ckStatusCode(resp) = 0
        Debug "No HTTP response was received. Check connectivity and TLS settings."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    If (success <> 1) OR (CkHttpResponse::ckStatusCode(resp) < 200) OR (CkHttpResponse::ckStatusCode(resp) >= 300)
        Debug "Request failed. HTTP " + Str(CkHttpResponse::ckStatusCode(resp))
        If CkHttpResponse::ckStatusCode(resp) = 429
            Debug "Rate limited. Retry-After: " + CkHttpResponse::ckGetHeaderField(resp,"Retry-After")
        EndIf

        ;  Inspect this response locally; it can contain private Jira data.
        Debug CkHttpResponse::ckBodyStr(resp)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoad(reply,CkHttpResponse::ckBodyStr(resp))
    If success <> 1
        Debug "Expected a JSON object in the response."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    If CkJsonObject::ckJsonTypeOf(reply,"fields.attachment") <> 4
        Debug "Expected an array: fields.attachment"
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    i.i = 0
    While (i < CkJsonObject::ckSizeOfArray(reply,"fields.attachment"))
        CkJsonObject::setCkI(reply, i)
        If CkJsonObject::ckStringOfEquals(reply,"fields.attachment[i].filename",attachmentName,1) = 1
            attachmentId = CkJsonObject::ckStringOf(reply,"fields.attachment[i].id")
            matches = matches + 1
        EndIf

        i = i + 1
    Wend
    If (matches <> 1) OR (attachmentId = "")
        Debug "Expected exactly one attachment with this filename. Matches: " + Str(matches)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        ProcedureReturn
    EndIf

    CkHttp::ckSetUrlVar(http,"attachmentId",attachmentId)
    CkHttp::setCkFollowRedirects(http, 0)
    CkHttp::setCkAccept(http, "*/*")
    CkStringBuilder::ckSetString(url,baseUrl)
    CkStringBuilder::ckAppend(url,"/rest/api/3/attachment/content/{$attachmentId}?redirect=false")
    success = CkHttp::ckHttpNoBody(http,"GET",CkStringBuilder::ckGetAsString(url),resp)
    If (CkHttpResponse::ckStatusCode(resp) = 302) OR (CkHttpResponse::ckStatusCode(resp) = 303) OR (CkHttpResponse::ckStatusCode(resp) = 307)
        location.i = CkStringBuilder::ckCreate()
        If location.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        CkStringBuilder::ckSetString(location,CkHttpResponse::ckGetHeaderField(resp,"Location"))
        If CkStringBuilder::ckStartsWith(location,"https://",0) <> 1
            Debug "Expected an HTTPS attachment redirect."
            CkHttp::ckDispose(http)
            CkStringBuilder::ckDispose(url)
            CkHttpResponse::ckDispose(resp)
            CkJsonObject::ckDispose(reply)
            CkStringBuilder::ckDispose(location)
            ProcedureReturn
        EndIf

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

        CkHttp::setCkRequireSslCertVerify(download, 1)
        ;  The signed URL authorizes this request; do not add Jira credentials.
        success = CkHttp::ckHttpNoBody(download,"GET",CkStringBuilder::ckGetAsString(location),resp)
    EndIf

    If (success <> 1) OR (CkHttpResponse::ckStatusCode(resp) <> 200)
        Debug "Attachment download failed. HTTP " + Str(CkHttpResponse::ckStatusCode(resp))
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        CkStringBuilder::ckDispose(location)
        CkHttp::ckDispose(download)
        ProcedureReturn
    EndIf

    success = CkHttpResponse::ckSaveBodyBinary(resp,localPath)
    If success <> 1
        Debug "Could not save the downloaded attachment."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(reply)
        CkStringBuilder::ckDispose(location)
        CkHttp::ckDispose(download)
        ProcedureReturn
    EndIf

    Debug "Saved attachment to " + localPath


    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(url)
    CkHttpResponse::ckDispose(resp)
    CkJsonObject::ckDispose(reply)
    CkStringBuilder::ckDispose(location)
    CkHttp::ckDispose(download)


    ProcedureReturn
EndProcedure