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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

  // Assumes Chilkat is unlocked. See Global Unlock Sample.

  final http = CkHttp();
  http.requireSslCertVerify = true;
  http.basicAuth = true;
  // Read these credentials from private application configuration.
  http.login = 'YOUR_ATLASSIAN_EMAIL';
  http.password = 'YOUR_API_TOKEN';
  final baseUrl = 'https://your-domain.atlassian.net';
  http.accept = 'application/json';
  final url = CkStringBuilder();
  final resp = CkHttpResponse();
  final reply = CkJsonObject();
  final issueKey = 'PROJECT-123';
  http.setUrlVar('issueKey', issueKey);
  final attachmentName = 'report.pdf';
  var attachmentId = '';
  final localPath = 'qa_output/report.pdf';
  var matches = 0;
  url.setString(baseUrl);
  url.append('/rest/api/3/issue/{\$issueKey}?fields=attachment');
  success = true;
  try {
    http.httpNoBody('GET', url.getAsString(), resp);
  } on ChilkatException {
    success = false;
  }
  if (resp.statusCode == 0) {
    print('No HTTP response was received. Check connectivity and TLS settings.');
    return;
  }

  if ((!success) || ((resp.statusCode < 200) || (resp.statusCode >= 300))) {
    print('Request failed. HTTP ${resp.statusCode}');
    if (resp.statusCode == 429) {
      print('Rate limited. Retry-After: ${resp.getHeaderField('Retry-After')}');
    }

    // Inspect this response locally; it can contain private Jira data.
    print(resp.bodyStr);
    return;
  }

  try {
    reply.load(resp.bodyStr);
  } on ChilkatException {
    print('Expected a JSON object in the response.');
    return;
  }

  if (reply.jsonTypeOf('fields.attachment') != 4) {
    print('Expected an array: fields.attachment');
    return;
  }

  var i = 0;
  while (i < reply.sizeOfArray('fields.attachment')) {
    reply.i = i;
    if (reply.stringOfEquals('fields.attachment[i].filename', attachmentName, true)) {
      attachmentId = reply.stringOf('fields.attachment[i].id');
      matches++;
    }

    i++;
  }

  if ((matches != 1) || (attachmentId == '')) {
    print('Expected exactly one attachment with this filename. Matches: $matches');
    return;
  }

  http.setUrlVar('attachmentId', attachmentId);
  http.followRedirects = false;
  http.accept = '*/*';
  url.setString(baseUrl);
  url.append('/rest/api/3/attachment/content/{\$attachmentId}?redirect=false');
  success = true;
  try {
    http.httpNoBody('GET', url.getAsString(), resp);
  } on ChilkatException {
    success = false;
  }
  if ((resp.statusCode == 302) || ((resp.statusCode == 303) || (resp.statusCode == 307))) {
    final location = CkStringBuilder();
    location.setString(resp.getHeaderField('Location'));
    if (!location.startsWith('https://', false)) {
      print('Expected an HTTPS attachment redirect.');
      return;
    }

    final download = CkHttp();
    download.requireSslCertVerify = true;
    // The signed URL authorizes this request; do not add Jira credentials.
    success = true;
    try {
      download.httpNoBody('GET', location.getAsString(), resp);
    } on ChilkatException {
      success = false;
    }
  }

  if ((!success) || (resp.statusCode != 200)) {
    print('Attachment download failed. HTTP ${resp.statusCode}');
    return;
  }

  try {
    resp.saveBodyBinary(localPath);
  } on ChilkatException {
    print('Could not save the downloaded attachment.');
    return;
  }

  print('Saved attachment to $localPath');
}