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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

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!

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#?

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

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 :) 

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

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!

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

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