Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to retrieve an issue attachments with the rest api ?

Hy, 

 

I'm currently building an application which simplify the view of our JIRA platform (I don't know the version) and I would like to know how I can retrieve an issue attachments ?

 

I'm using the rest api with the search capability for fetching the issues but the returned json doesn't contains any attachments.

 

Thanks by advance ^^

7 answers

5 votes
K_ Yamamoto Atlassian Team Aug 29, 2018

Hi there,

Specify the following key-value pair to your query:

"fields": ["attachment"]

You can use this property option to almost any endpoint responding issue(s).

Then you can retrieve the URL which points the actual attachment file like below:

{
// ...
"attachment": [
{
"self": "https://example.atlassian.net/rest/api/2/attachment/10000",
"id": "10000",
"filename": "screenshot.png",
"author": { },
"created": "2018-08-29T16:12:43.716+0900",
"size": 213755,
"mimeType": "image/png",
"content": "https://example.atlassian.net/secure/attachment/10000/screenshot.png",
"thumbnail": "https://example.atlassian.net/secure/thumbnail/10000/screenshot.png"
}
],
// ...
}

Besides, plese see also my snippet Download files attached to the issues filtered by JQL for reference.

Hope it helps!

Specify the following key-value pair to your query:

"fields": ["attachment"]

Do you know if the returned array is ordered by attachment name, ID or something else?

Thanks in advance.

Cheers.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessor

def attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor)
def temporaryAttachmentDirectory = attachmentDirectoryAccessor.getTemporaryAttachmentDirectory()
def check=false
MutableIssue mutableIssue = (MutableIssue)issue

def newAttachmentNames =mutableIssue.getModifiedFields().get(IssueFieldConstants.ATTACHMENT)?.newValue
newAttachmentNames.each { String filename ->
log.debug "File text:" + new File(temporaryAttachmentDirectory, filename).text
check=true
}

return check

Attachments info don't appear in search results by default. But can be retrieved if you explicitly name it in 'fields' parameter. For example:

rest/api/2/search?jql=project=CRM+order+by+cf[10002]+asc&maxResults=100&fields=attachment

Unfortunately the result of this query will not include all other fields, but you can add in the 'fields' parameter all the fields you are going to work with.

I was looking for a REST endpoint that would return all attachments for a given issue key, however, this endpoint does not exist (could have easily been implemented as GET to the same endpoint that creates attachment, anyway...)

But wow!  I didn't know the rest/api/2/search endpoint could be used in this way!  

This is great!  I basically can create any custom query that is possible with jql using this "search" endpoint!

Thank you!

Victor if you know the given key already, you don't need to search, it will be faster to access the actual endpoint for this functionality (which does in fact exist)

 

Invoke-JIRARequest https://asdsjira.4yoursoul.net/rest/api/2/issue/ISSUEID-200?fields=attachment -- returns an object that includes fields.attachment and within that there is the attachment name, url, id, author, date, file type, etc.; the attachments themselves are available at the URLs stated in that returned data that you just access by standard GET requests.

Like Victor Cheung likes this

Thanks, Dane!  This is even better!  I will use the issue endpoint as you recommended!

Cheers!

I was searching for a succinct way to find the attachment IDs on a specific issue. Note the "expand=attachment"... this is key!

curl -u 'user:password' -X GET -H "Content-Type: application/json" https://host/rest/api/2/issue/ISSUE-123?expand=attachment  | jq  .fields.attachment[].id

I found a simple way to do what I want smile

 

A simple call to this url /secure/attachmentzip/{issueId}.zip allow you to download the issue attachments as zip.

 

Too bad that the rest api doesn't tell if or not an issue has attachments or not in order to avoid the call to the url if no attachments are present.

Thanks for your brilliant suggestion. It helps me a lot. Big thumb up.

Regarding the method to check if there is an attachment or not. I have a suggestion as below:

/rest/api/2/issue/{issueIdOrKey}/?fields=attachment

this will return the list of attachment. I think it can be use to check if there is an attachment or not :) 

Like # people like this

@Phong Nguyen - Hey, I was working on something similar, I want to download all the attachments in an issue, but couldn't find any GET method in the API documentation. I tried using @Vincent Vergnolle approach 'A simple call to this url /secure/attachmentzip/{issueId}.zip allow you to download the issue attachments as zip.', but it didn't work for me. It seems from your above comment that you were able to utilize it, please help me with that. 

@Phong Nguyen and @Vincent Vergnolle  - I have a similar requirement as yours, I tried using the '/secure/attachmentzip/{issueId}.zip ' approach, but it didn't work. Please help me out with this.

hi @Mosaddique Rafi ,

What seems to be your road block? I tried again with postman and it worked for me. 

Please make sure that your issue for testing has an attachment. 

Also, the authentication of the API may require you to have a token from your Atlassian account.

Hope that it helps.

@Phong Nguyen Thanks for replying.
I am simply using an authenticated session and directly using the URI in the browser. I am testing this to incorporate in my RPA project.
The other URI work as expected, such as 'https://companydomain/rest/api/2/issue/issue-ID/?fields=attachment' -  gives me the list of attachments in the issue.
But when I try 'https://companydomain/secure/attachment/issueID/file.zip' - gives me a 404 or dead link. Am I writing the URI correctly to download all the attachments?

The URI I used was domain/secure/attachmentzip/{issueId}.zip 

I am sorry, I pasted the wrong URI last time. I used the same URI which you have mentioned.

https://domain/secure/attachmentzip/IssueID.zip
This returns "

Oops, you've found a dead link.

"

@Phong Nguyen , Is there any documentation or reference for this particular '/secure/attachmentzip/{issueId}.zip' URI?
I tried to look at the documentations but didn't find any API for downloading the attachments. 

I didn't find any documents beside this thread. 

Sorry that I have no clue why it didn't work on your side. 

Hey, don't know whether this helps or not, but I also found this problem in one of the projects I'm working on, and I use python, this code I created to download attachments on issues in jira.

from jira import JIRA

import json

import base64

import requests

 

JIRA_URL = "YOUR_JIRA_HOST"

JIRA_EMAIL = "YOUR_JIRA_EMAIL"

JIRA_API_KEY = "YOUR_JIRA_TOKEN"

jira
= JIRA(JIRA_URL, basic_auth = (JIRA_EMAIL, JIRA_API_KEY))

msg = f"{JIRA_EMAIL}:{JIRA_API_KEY}"

token_base64 = base64.b64encode(msg.encode('ascii'))

 

main_ticket = "" # response from (JIRA_URL}/rest/api/3/issue/{issueId}"

for attachment in main_ticket["fields"]["attachment"]:

    content = attachment["content"]

    file_name = attachment["filename"]

    headers = {"Authorization" : "Basic "+token_base64.decode('ascii')}

    reqs = requests.get(url=str(content), allow_redirects=True, headers=headers)

    print(json.loads(reqs.text)) # this is only example while processing json data, you can change it to write to file for any other mimetype data.

Thanks for the answer Paula,

 

In your example, there is a piece of code that download an attachment but the method accept the content uri. My problem is that I can't have the content uri because it's based on an attachment id and I doesn't have it.

 

I supposed that the uri is like /secure/attachment/{id} but I doesn't have the id and that what I'm looking for. Getting the attachments ids of an issue for downloading them.

 

That's what I've understand from this post (approved answer).

 

Is there is another way to proceed ?

you can retrieve the ID of the attachments associated w/ a task (and their filename and other meta data) by retrieving the issue from https://jira/rest/api/latest/issue/  (they will be listed under fields.attachment.id)

Like Tinu Panicker likes this
0 votes

Hi Vincent,

Please check How do I upload attachment to JIRA Issue via REST API?, there's an example of how to download attachments.

Hope it works for you.

Thanks and regards,
Paula Silveira

do you have codes in C#?

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you