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

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.BasicAuth = True
;  Read these credentials from private application configuration.
$oHttp.Login = "YOUR_ATLASSIAN_EMAIL"
$oHttp.Password = "YOUR_API_TOKEN"
Local $sBaseUrl = "https://your-domain.atlassian.net"
$oHttp.Accept = "application/json"
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oResp = ObjCreate("Chilkat.HttpResponse")
$oReply = ObjCreate("Chilkat.JsonObject")
Local $sIssueKey = "PROJECT-123"
$oHttp.SetUrlVar("issueKey",$sIssueKey)
Local $sAttachmentName = "report.pdf"
Local $sAttachmentId = ""
Local $sLocalPath = "qa_output/report.pdf"
Local $iMatches = 0
$oUrl.SetString($sBaseUrl)
$oUrl.Append("/rest/api/3/issue/{$issueKey}?fields=attachment")
$bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$oResp)
If ($oResp.StatusCode = 0) Then
    ConsoleWrite("No HTTP response was received. Check connectivity and TLS settings." & @CRLF)
    Exit
EndIf

If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
    ConsoleWrite("Request failed. HTTP " & $oResp.StatusCode & @CRLF)
    If ($oResp.StatusCode = 429) Then
        ConsoleWrite("Rate limited. Retry-After: " & $oResp.GetHeaderField("Retry-After") & @CRLF)
    EndIf

    ;  Inspect this response locally; it can contain private Jira data.
    ConsoleWrite($oResp.BodyStr & @CRLF)
    Exit
EndIf

$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a JSON object in the response." & @CRLF)
    Exit
EndIf

If ($oReply.JsonTypeOf("fields.attachment") <> 4) Then
    ConsoleWrite("Expected an array: fields.attachment" & @CRLF)
    Exit
EndIf

Local $i = 0
While ($i < $oReply.SizeOfArray("fields.attachment"))
    $oReply.I = $i
    If ($oReply.StringOfEquals("fields.attachment[i].filename",$sAttachmentName,True) = True) Then
        $sAttachmentId = $oReply.StringOf("fields.attachment[i].id")
        $iMatches = $iMatches + 1
    EndIf

    $i = $i + 1
Wend
If (($iMatches <> 1) Or ($sAttachmentId = "")) Then
    ConsoleWrite("Expected exactly one attachment with this filename. Matches: " & $iMatches & @CRLF)
    Exit
EndIf

$oHttp.SetUrlVar("attachmentId",$sAttachmentId)
$oHttp.FollowRedirects = False
$oHttp.Accept = "*/*"
$oUrl.SetString($sBaseUrl)
$oUrl.Append("/rest/api/3/attachment/content/{$attachmentId}?redirect=false")
$bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$oResp)
If (($oResp.StatusCode = 302) Or ($oResp.StatusCode = 303) Or ($oResp.StatusCode = 307)) Then
    $oLocation = ObjCreate("Chilkat.StringBuilder")
    $oLocation.SetString($oResp.GetHeaderField("Location"))
    If ($oLocation.StartsWith("https://",False) <> True) Then
        ConsoleWrite("Expected an HTTPS attachment redirect." & @CRLF)
        Exit
    EndIf

    $oDownload = ObjCreate("Chilkat.Http")
    $oDownload.RequireSslCertVerify = True
    ;  The signed URL authorizes this request; do not add Jira credentials.
    $bSuccess = $oDownload.HttpNoBody("GET",$oLocation.GetAsString(),$oResp)
EndIf

If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
    ConsoleWrite("Attachment download failed. HTTP " & $oResp.StatusCode & @CRLF)
    Exit
EndIf

$bSuccess = $oResp.SaveBodyBinary($sLocalPath)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not save the downloaded attachment." & @CRLF)
    Exit
EndIf

ConsoleWrite("Saved attachment to " & $sLocalPath & @CRLF)