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

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @iTmp2 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @http, 'BasicAuth', 1
    --  Read these credentials from private application configuration.
    EXEC sp_OASetProperty @http, 'Login', 'YOUR_ATLASSIAN_EMAIL'
    EXEC sp_OASetProperty @http, 'Password', 'YOUR_API_TOKEN'
    DECLARE @baseUrl nvarchar(4000)
    SELECT @baseUrl = 'https://your-domain.atlassian.net'
    EXEC sp_OASetProperty @http, 'Accept', 'application/json'
    DECLARE @url int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    DECLARE @reply int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @reply OUT

    DECLARE @issueKey nvarchar(4000)
    SELECT @issueKey = 'PROJECT-123'
    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'issueKey', @issueKey
    DECLARE @attachmentName nvarchar(4000)
    SELECT @attachmentName = 'report.pdf'
    DECLARE @attachmentId nvarchar(4000)
    SELECT @attachmentId = ''
    DECLARE @localPath nvarchar(4000)
    SELECT @localPath = 'qa_output/report.pdf'
    DECLARE @matches int
    SELECT @matches = 0
    EXEC sp_OAMethod @url, 'SetString', @success OUT, @baseUrl
    EXEC sp_OAMethod @url, 'Append', @success OUT, '/rest/api/3/issue/{$issueKey}?fields=attachment'
    EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @sTmp0, @resp
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN

        PRINT 'No HTTP response was received. Check connectivity and TLS settings.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @url
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
    IF (@success <> 1) or (@iTmp0 < 200) or (@iTmp1 >= 300)
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'Request failed. HTTP ' + @iTmp0
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 = 429
          BEGIN

            EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
            PRINT 'Rate limited. Retry-After: ' + @sTmp0
          END
        --  Inspect this response locally; it can contain private Jira data.
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @url
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @reply, 'Load', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Expected a JSON object in the response.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @url
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        RETURN
      END
    EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'fields.attachment'
    IF @iTmp0 <> 4
      BEGIN

        PRINT 'Expected an array: fields.attachment'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @url
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        RETURN
      END
    DECLARE @i int
    SELECT @i = 0
    EXEC sp_OAMethod @reply, 'SizeOfArray', @iTmp0 OUT, 'fields.attachment'
    WHILE (@i < @iTmp0)
      BEGIN
        EXEC sp_OASetProperty @reply, 'I', @i
        EXEC sp_OAMethod @reply, 'StringOfEquals', @iTmp0 OUT, 'fields.attachment[i].filename', @attachmentName, 1
        IF @iTmp0 = 1
          BEGIN
            EXEC sp_OAMethod @reply, 'StringOf', @attachmentId OUT, 'fields.attachment[i].id'
            SELECT @matches = @matches + 1
          END
        SELECT @i = @i + 1
      END
    IF (@matches <> 1) or (@attachmentId = '')
      BEGIN

        PRINT 'Expected exactly one attachment with this filename. Matches: ' + @matches
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @url
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        RETURN
      END
    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'attachmentId', @attachmentId
    EXEC sp_OASetProperty @http, 'FollowRedirects', 0
    EXEC sp_OASetProperty @http, 'Accept', '*/*'
    EXEC sp_OAMethod @url, 'SetString', @success OUT, @baseUrl
    EXEC sp_OAMethod @url, 'Append', @success OUT, '/rest/api/3/attachment/content/{$attachmentId}?redirect=false'
    EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @sTmp0, @resp
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp2 OUT
    IF (@iTmp0 = 302) or (@iTmp1 = 303) or (@iTmp2 = 307)
      BEGIN
        DECLARE @location int
        EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @location OUT

        EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Location'
        EXEC sp_OAMethod @location, 'SetString', @success OUT, @sTmp0
        EXEC sp_OAMethod @location, 'StartsWith', @iTmp0 OUT, 'https://', 0
        IF @iTmp0 <> 1
          BEGIN

            PRINT 'Expected an HTTPS attachment redirect.'
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @url
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @reply
            EXEC @hr = sp_OADestroy @location
            RETURN
          END
        DECLARE @download int
        EXEC @hr = sp_OACreate 'Chilkat.Http', @download OUT

        EXEC sp_OASetProperty @download, 'RequireSslCertVerify', 1
        --  The signed URL authorizes this request; do not add Jira credentials.
        EXEC sp_OAMethod @location, 'GetAsString', @sTmp0 OUT
        EXEC sp_OAMethod @download, 'HttpNoBody', @success OUT, 'GET', @sTmp0, @resp
      END
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF (@success <> 1) or (@iTmp0 <> 200)
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'Attachment download failed. HTTP ' + @iTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @url
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        EXEC @hr = sp_OADestroy @location
        EXEC @hr = sp_OADestroy @download
        RETURN
      END
    EXEC sp_OAMethod @resp, 'SaveBodyBinary', @success OUT, @localPath
    IF @success <> 1
      BEGIN

        PRINT 'Could not save the downloaded attachment.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @url
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        EXEC @hr = sp_OADestroy @location
        EXEC @hr = sp_OADestroy @download
        RETURN
      END

    PRINT 'Saved attachment to ' + @localPath

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @url
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @reply
    EXEC @hr = sp_OADestroy @location
    EXEC @hr = sp_OADestroy @download


END
GO