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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
String sBaseUrl
Handle hoUrl
Variant vResp
Handle hoResp
Handle hoReply
String sIssueKey
String sAttachmentName
String sAttachmentId
String sLocalPath
Integer iMatches
Integer i
Handle hoLocation
Handle hoDownload
String sTemp1
Integer iTemp1
Integer iTemp2
Integer iTemp3
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComBasicAuth Of hoHttp To True
// Read these credentials from private application configuration.
Set ComLogin Of hoHttp To "YOUR_ATLASSIAN_EMAIL"
Set ComPassword Of hoHttp To "YOUR_API_TOKEN"
Move "https://your-domain.atlassian.net" To sBaseUrl
Set ComAccept Of hoHttp To "application/json"
Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
If (Not(IsComObjectCreated(hoUrl))) Begin
Send CreateComObject of hoUrl
End
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatJsonObject)) To hoReply
If (Not(IsComObjectCreated(hoReply))) Begin
Send CreateComObject of hoReply
End
Move "PROJECT-123" To sIssueKey
Get ComSetUrlVar Of hoHttp "issueKey" sIssueKey To iSuccess
Move "report.pdf" To sAttachmentName
Move "" To sAttachmentId
Move "qa_output/report.pdf" To sLocalPath
Move 0 To iMatches
Get ComSetString Of hoUrl sBaseUrl To iSuccess
Get ComAppend Of hoUrl "/rest/api/3/issue/{$issueKey}?fields=attachment" To iSuccess
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "No HTTP response was received. Check connectivity and TLS settings."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Request failed. HTTP " iTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 429) Begin
Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
Showln "Rate limited. Retry-After: " sTemp1
End
// Inspect this response locally; it can contain private Jira data.
Get ComBodyStr Of hoResp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a JSON object in the response."
Procedure_Return
End
Get ComJsonTypeOf Of hoReply "fields.attachment" To iTemp1
If (iTemp1 <> 4) Begin
Showln "Expected an array: fields.attachment"
Procedure_Return
End
Move 0 To i
While (i < (ComSizeOfArray(hoReply, "fields.attachment")))
Set ComI Of hoReply To i
Get ComStringOfEquals Of hoReply "fields.attachment[i].filename" sAttachmentName True To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoReply "fields.attachment[i].id" To sAttachmentId
Move (iMatches + 1) To iMatches
End
Move (i + 1) To i
Loop
If ((iMatches <> 1) Or (sAttachmentId = "")) Begin
Showln "Expected exactly one attachment with this filename. Matches: " iMatches
Procedure_Return
End
Get ComSetUrlVar Of hoHttp "attachmentId" sAttachmentId To iSuccess
Set ComFollowRedirects Of hoHttp To False
Set ComAccept Of hoHttp To "*/*"
Get ComSetString Of hoUrl sBaseUrl To iSuccess
Get ComAppend Of hoUrl "/rest/api/3/attachment/content/{$attachmentId}?redirect=false" To iSuccess
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
Get ComStatusCode Of hoResp To iTemp3
If ((iTemp1 = 302) Or (iTemp2 = 303) Or (iTemp3 = 307)) Begin
Get Create (RefClass(cComChilkatStringBuilder)) To hoLocation
If (Not(IsComObjectCreated(hoLocation))) Begin
Send CreateComObject of hoLocation
End
Get ComGetHeaderField Of hoResp "Location" To sTemp1
Get ComSetString Of hoLocation sTemp1 To iSuccess
Get ComStartsWith Of hoLocation "https://" False To bTemp1
If (bTemp1 <> True) Begin
Showln "Expected an HTTPS attachment redirect."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoDownload
If (Not(IsComObjectCreated(hoDownload))) Begin
Send CreateComObject of hoDownload
End
Set ComRequireSslCertVerify Of hoDownload To True
// The signed URL authorizes this request; do not add Jira credentials.
Get ComGetAsString Of hoLocation To sTemp1
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoDownload "GET" sTemp1 vResp To iSuccess
End
Get ComStatusCode Of hoResp To iTemp1
If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Attachment download failed. HTTP " iTemp1
Procedure_Return
End
Get ComSaveBodyBinary Of hoResp sLocalPath To iSuccess
If (iSuccess <> True) Begin
Showln "Could not save the downloaded attachment."
Procedure_Return
End
Showln "Saved attachment to " sLocalPath
End_Procedure