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 Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set http [new_CkHttp]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_BasicAuth $http 1
# Read these credentials from private application configuration.
CkHttp_put_Login $http "YOUR_ATLASSIAN_EMAIL"
CkHttp_put_Password $http "YOUR_API_TOKEN"
set baseUrl "https://your-domain.atlassian.net"
CkHttp_put_Accept $http "application/json"
set url [new_CkStringBuilder]
set resp [new_CkHttpResponse]
set reply [new_CkJsonObject]
set issueKey "PROJECT-123"
CkHttp_SetUrlVar $http "issueKey" $issueKey
set attachmentName "report.pdf"
set attachmentId ""
set localPath "qa_output/report.pdf"
set matches 0
CkStringBuilder_SetString $url $baseUrl
CkStringBuilder_Append $url "/rest/api/3/issue/{$issueKey}?fields=attachment"
set success [CkHttp_HttpNoBody $http "GET" [CkStringBuilder_getAsString $url] $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
puts "No HTTP response was received. Check connectivity and TLS settings."
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
if {expr [$success != 1] || [expr [[CkHttpResponse_get_StatusCode $resp] < 200] || [[CkHttpResponse_get_StatusCode $resp] >= 300]]} then {
puts "Request failed. HTTP [CkHttpResponse_get_StatusCode $resp]"
if {[CkHttpResponse_get_StatusCode $resp] == 429} then {
puts "Rate limited. Retry-After: [CkHttpResponse_getHeaderField $resp Retry-After]"
}
# Inspect this response locally; it can contain private Jira data.
puts [CkHttpResponse_bodyStr $resp]
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
set success [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
puts "Expected a JSON object in the response."
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
if {[CkJsonObject_JsonTypeOf $reply "fields.attachment"] != 4} then {
puts "Expected an array: fields.attachment"
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
set i 0
while {$i < [CkJsonObject_SizeOfArray $reply "fields.attachment"]} {
CkJsonObject_put_I $reply $i
if {[CkJsonObject_StringOfEquals $reply "fields.attachment[i].filename" $attachmentName 1] == 1} then {
set attachmentId [CkJsonObject_stringOf $reply "fields.attachment[i].id"]
set matches [expr $matches + 1]
}
set i [expr $i + 1]
}
if {expr [$matches != 1] || [$attachmentId == ""]} then {
puts "Expected exactly one attachment with this filename. Matches: $matches"
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
CkHttp_SetUrlVar $http "attachmentId" $attachmentId
CkHttp_put_FollowRedirects $http 0
CkHttp_put_Accept $http "*/*"
CkStringBuilder_SetString $url $baseUrl
CkStringBuilder_Append $url "/rest/api/3/attachment/content/{$attachmentId}?redirect=false"
set success [CkHttp_HttpNoBody $http "GET" [CkStringBuilder_getAsString $url] $resp]
if {expr [[CkHttpResponse_get_StatusCode $resp] == 302] || [expr [[CkHttpResponse_get_StatusCode $resp] == 303] || [[CkHttpResponse_get_StatusCode $resp] == 307]]} then {
set location [new_CkStringBuilder]
CkStringBuilder_SetString $location [CkHttpResponse_getHeaderField $resp "Location"]
if {[CkStringBuilder_StartsWith $location "https://" 0] != 1} then {
puts "Expected an HTTPS attachment redirect."
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkStringBuilder $location
exit
}
set download [new_CkHttp]
CkHttp_put_RequireSslCertVerify $download 1
# The signed URL authorizes this request; do not add Jira credentials.
set success [CkHttp_HttpNoBody $download "GET" [CkStringBuilder_getAsString $location] $resp]
}
if {expr [$success != 1] || [[CkHttpResponse_get_StatusCode $resp] != 200]} then {
puts "Attachment download failed. HTTP [CkHttpResponse_get_StatusCode $resp]"
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkStringBuilder $location
delete_CkHttp $download
exit
}
set success [CkHttpResponse_SaveBodyBinary $resp $localPath]
if {$success != 1} then {
puts "Could not save the downloaded attachment."
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkStringBuilder $location
delete_CkHttp $download
exit
}
puts "Saved attachment to $localPath"
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkStringBuilder $location
delete_CkHttp $download