Download attachment from REST API

Hello,
I would like to check if JIRA cloud REST API is suitable for download an attachment?
The Doc describes abilities to upload (POST) an attachment to issue, get its metadata but no download.

Is there is REST API for download?
Is it possible to download from client app without server and Jira-connect?

Thanks for your help

Hello @ErezAvny!
Both Get Issue and Get attachment metadata contain an attachment’s content field which is URL for this attachment.
All you have to do to download it is to make a GET request on this URL.

Example:
I’ve called the Get attachment metadata API and got this response

 {
  "id": 10000,
  "self": "https://your-domain.atlassian.net/rest/api/3/attachments/10000",
  "filename": "picture.jpg",
  "author": {
    "self": "https://your-domain.atlassian.net/rest/api/3/user?accountId=5b10a2844c20165700ede21g",
    "key": "",
    "accountId": "5b10a2844c20165700ede21g",
    "name": "",
    "avatarUrls": {
      "48x48": "https://avatar-management--avatars.server-location.prod.public.atl-paas.net/initials/MK-5.png?size=48&s=48",
      "24x24": "https://avatar-management--avatars.server-location.prod.public.atl-paas.net/initials/MK-5.png?size=24&s=24",
      "16x16": "https://avatar-management--avatars.server-location.prod.public.atl-paas.net/initials/MK-5.png?size=16&s=16",
      "32x32": "https://avatar-management--avatars.server-location.prod.public.atl-paas.net/initials/MK-5.png?size=32&s=32"
    },
    "displayName": "Mia Krystof",
    "active": false
  },
  "created": "2020-08-17T06:46:11.060+0000",
  "size": 23123,
  "mimeType": "image/jpeg",
  "content": "https://your-domain.atlassian.net/jira/secure/attachments/10000/picture.jpg",
  "thumbnail": "https://your-domain.atlassian.net/jira/secure/thumbnail/10000/picture.jpg"
}

You may notice that it contains content field with value of https://your-domain.atlassian.net/jira/secure/attachments/10000/picture.jpg

Now I can make a GET https://your-domain.atlassian.net/jira/secure/attachments/10000/picture.jpg request to fetch this attachment.

I hope that answers your question!
Regards,
Maciej

hello @MaciejStanuch ,
thanks for quick replay.

I try follow your guide but when I try to fetch the file with GET (inside the Jira cloud app) the response is redirect to login page and got http/https mismatch content.

Request with fetch:

    fetch('https://{my-domain.atlassian.net}/secure/attachment/{id}/{filename}.json' {
      headers: {
        'Content-Type': 'application/json',
      }
    })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (ex) {
        console.log('Parsing failed:', ex);
      });

The request redirect to:
https://{my-domain}.atlassian.net/login.jsp?permissionViolation=true&os_destination=%2Fsecure%2Fattachment%2F{attachment-id}%2F{file-name}.json&page_caps=&user_role=


And got this error in network panel:

Access to fetch at 'https://{my-domain}.atlassian.net/login.jsp?permissionViolation=true&os_destination=%2Fsecure%2Fattachment%2F{attachment-id}%2F{file-name}.json&page_caps=&user_role=' (redirected from 'https://{my-domain}.atlassian.net/secure/attachment/{attachment-id}/{file-name}.json') from origin 'https://{tunnel-id}.ngrok.io' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

Can you help, please?

thanks,
Erez

I think you’re missing Authentication header. To make this kind of request you have to be authenticated to your Jira instance. You can do it by, for example, adding Basic Auth header to the request or calling API from authenticated Connect Application.

Some more info about how to add Basic Auth: Basic auth for REST APIs

Some more info about Connect Addons:
https://developer.atlassian.com/cloud/jira/platform/security-for-connect-apps/

You should be able to find all the details at the before-mentioned pages.

Cheers!
Maciej

I am having a similar issue. I tried authenticating using Basic authentication which worked for me while getting the metadata for the attachments.
It is throwing a 400 response code to my GET request.
Let me know how I can get around this.

1 Like

Hello, what is the process for doing this via oauth? I have the following node code

const fileData = await Superagent(attachment.content).set(
          'Authorization',
          `Bearer: ${jira.accessToken}`
        );

however this results in a 404 not found and seems to be redirecting me to the login url

@MaciejStanuch thoughts? is this workflow supported via oauth? If not are there any plans to?

Hello, as mentioned @MaciejStanuch , you need to fetch the file content with authorization header (basic auth)
here’s below exp using postman
1- first get the attchment meta data


2- then get the file content with same authorization header
GET https//:fileurl.pdf

Hello @ErezAvny ,

I think the easiest way to download attachment/s programaticaly is to use wget command. Please see example below:

wget -d --header="X-Auth-Token: XXXXXXXXXXXXXXXXXXXX" https://your_jira_url/secure/attachmentzip/attachement.jpg --no-check-certificate

If you’ve got any loadbalancer in front it might be required to use cookies parameter:
--load-cookies cookies.txt

Regards,
Szymon

Hi, I tried to use this method but got an error

null for uri: status-code’: 404

Can you please help me out.
I am trying to download pdf.