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 Delphi DLL Downloads
var
success: Boolean;
http: HCkHttp;
baseUrl: PWideChar;
url: HCkStringBuilder;
resp: HCkHttpResponse;
reply: HCkJsonObject;
issueKey: PWideChar;
attachmentName: PWideChar;
attachmentId: PWideChar;
localPath: PWideChar;
matches: Integer;
i: Integer;
location: HCkStringBuilder;
download: HCkHttp;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putBasicAuth(http,True);
// Read these credentials from private application configuration.
CkHttp_putLogin(http,'YOUR_ATLASSIAN_EMAIL');
CkHttp_putPassword(http,'YOUR_API_TOKEN');
baseUrl := 'https://your-domain.atlassian.net';
CkHttp_putAccept(http,'application/json');
url := CkStringBuilder_Create();
resp := CkHttpResponse_Create();
reply := CkJsonObject_Create();
issueKey := 'PROJECT-123';
CkHttp_SetUrlVar(http,'issueKey',issueKey);
attachmentName := 'report.pdf';
attachmentId := '';
localPath := 'qa_output/report.pdf';
matches := 0;
CkStringBuilder_SetString(url,baseUrl);
CkStringBuilder_Append(url,'/rest/api/3/issue/{$issueKey}?fields=attachment');
success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(url),resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add('No HTTP response was received. Check connectivity and TLS settings.');
Exit;
end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
begin
Memo1.Lines.Add('Request failed. HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
if (CkHttpResponse_getStatusCode(resp) = 429) then
begin
Memo1.Lines.Add('Rate limited. Retry-After: ' + CkHttpResponse__getHeaderField(resp,'Retry-After'));
end;
// Inspect this response locally; it can contain private Jira data.
Memo1.Lines.Add(CkHttpResponse__bodyStr(resp));
Exit;
end;
success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a JSON object in the response.');
Exit;
end;
if (CkJsonObject_JsonTypeOf(reply,'fields.attachment') <> 4) then
begin
Memo1.Lines.Add('Expected an array: fields.attachment');
Exit;
end;
i := 0;
while (i < CkJsonObject_SizeOfArray(reply,'fields.attachment')) do
begin
CkJsonObject_putI(reply,i);
if (CkJsonObject_StringOfEquals(reply,'fields.attachment[i].filename',attachmentName,True) = True) then
begin
attachmentId := CkJsonObject__stringOf(reply,'fields.attachment[i].id');
matches := matches + 1;
end;
i := i + 1;
end;
if ((matches <> 1) or (attachmentId = '')) then
begin
Memo1.Lines.Add('Expected exactly one attachment with this filename. Matches: ' + IntToStr(matches));
Exit;
end;
CkHttp_SetUrlVar(http,'attachmentId',attachmentId);
CkHttp_putFollowRedirects(http,False);
CkHttp_putAccept(http,'*/*');
CkStringBuilder_SetString(url,baseUrl);
CkStringBuilder_Append(url,'/rest/api/3/attachment/content/{$attachmentId}?redirect=false');
success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(url),resp);
if ((CkHttpResponse_getStatusCode(resp) = 302) or (CkHttpResponse_getStatusCode(resp) = 303) or (CkHttpResponse_getStatusCode(resp) = 307)) then
begin
location := CkStringBuilder_Create();
CkStringBuilder_SetString(location,CkHttpResponse__getHeaderField(resp,'Location'));
if (CkStringBuilder_StartsWith(location,'https://',False) <> True) then
begin
Memo1.Lines.Add('Expected an HTTPS attachment redirect.');
Exit;
end;
download := CkHttp_Create();
CkHttp_putRequireSslCertVerify(download,True);
// The signed URL authorizes this request; do not add Jira credentials.
success := CkHttp_HttpNoBody(download,'GET',CkStringBuilder__getAsString(location),resp);
end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
begin
Memo1.Lines.Add('Attachment download failed. HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
Exit;
end;
success := CkHttpResponse_SaveBodyBinary(resp,localPath);
if (success <> True) then
begin
Memo1.Lines.Add('Could not save the downloaded attachment.');
Exit;
end;
Memo1.Lines.Add('Saved attachment to ' + localPath);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkStringBuilder_Dispose(location);
CkHttp_Dispose(download);