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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
string ls_BaseUrl
oleobject loo_Url
oleobject loo_Resp
oleobject loo_Reply
string ls_IssueKey
string ls_AttachmentName
string ls_AttachmentId
string ls_LocalPath
long li_Matches
long i
oleobject loo_Location
oleobject loo_Download
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Http.RequireSslCertVerify = 1
loo_Http.BasicAuth = 1
// Read these credentials from private application configuration.
loo_Http.Login = "YOUR_ATLASSIAN_EMAIL"
loo_Http.Password = "YOUR_API_TOKEN"
ls_BaseUrl = "https://your-domain.atlassian.net"
loo_Http.Accept = "application/json"
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")
ls_IssueKey = "PROJECT-123"
loo_Http.SetUrlVar("issueKey",ls_IssueKey)
ls_AttachmentName = "report.pdf"
ls_AttachmentId = ""
ls_LocalPath = "qa_output/report.pdf"
li_Matches = 0
loo_Url.SetString(ls_BaseUrl)
loo_Url.Append("/rest/api/3/issue/{$issueKey}?fields=attachment")
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "No HTTP response was received. Check connectivity and TLS settings."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Request failed. HTTP " + string(loo_Resp.StatusCode)
if loo_Resp.StatusCode = 429 then
Write-Debug "Rate limited. Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
end if
// Inspect this response locally; it can contain private Jira data.
Write-Debug loo_Resp.BodyStr
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
return
end if
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a JSON object in the response."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
return
end if
if loo_Reply.JsonTypeOf("fields.attachment") <> 4 then
Write-Debug "Expected an array: fields.attachment"
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
return
end if
i = 0
do while (i < loo_Reply.SizeOfArray("fields.attachment"))
loo_Reply.I = i
if loo_Reply.StringOfEquals("fields.attachment[i].filename",ls_AttachmentName,1) = 1 then
ls_AttachmentId = loo_Reply.StringOf("fields.attachment[i].id")
li_Matches = li_Matches + 1
end if
i = i + 1
loop
if (li_Matches <> 1) OR (ls_AttachmentId = "") then
Write-Debug "Expected exactly one attachment with this filename. Matches: " + string(li_Matches)
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
return
end if
loo_Http.SetUrlVar("attachmentId",ls_AttachmentId)
loo_Http.FollowRedirects = 0
loo_Http.Accept = "*/*"
loo_Url.SetString(ls_BaseUrl)
loo_Url.Append("/rest/api/3/attachment/content/{$attachmentId}?redirect=false")
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
if (loo_Resp.StatusCode = 302) OR (loo_Resp.StatusCode = 303) OR (loo_Resp.StatusCode = 307) then
loo_Location = create oleobject
li_rc = loo_Location.ConnectToNewObject("Chilkat.StringBuilder")
loo_Location.SetString(loo_Resp.GetHeaderField("Location"))
if loo_Location.StartsWith("https://",0) <> 1 then
Write-Debug "Expected an HTTPS attachment redirect."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Location
return
end if
loo_Download = create oleobject
li_rc = loo_Download.ConnectToNewObject("Chilkat.Http")
loo_Download.RequireSslCertVerify = 1
// The signed URL authorizes this request; do not add Jira credentials.
li_Success = loo_Download.HttpNoBody("GET",loo_Location.GetAsString(),loo_Resp)
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
Write-Debug "Attachment download failed. HTTP " + string(loo_Resp.StatusCode)
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Location
destroy loo_Download
return
end if
li_Success = loo_Resp.SaveBodyBinary(ls_LocalPath)
if li_Success <> 1 then
Write-Debug "Could not save the downloaded attachment."
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Location
destroy loo_Download
return
end if
Write-Debug "Saved attachment to " + ls_LocalPath
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply
destroy loo_Location
destroy loo_Download