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

How to add attachment to Jira issue to a specific attachment category via Rest API

Shweta August 6, 2019

The attachment section of my issue has 2 categories, lets say 'Standard' and 'Data'. This URL: 'http://myhost/rest/api/2/issue/TEST-123/attachments' attaches my files in 'Standard' category. What change needs to be made in the URL or the body in order for files to be attached in 'Data' category

2 answers

0 votes
Shweta August 8, 2019

First API Call worked, I got the Category ID. For the second one I got API Call Error (500) . What could be the reason for that?

Endpoint :- 'POST/rest/attach-cat/1.0/attachments'

Parameter  :- issueKey=Test-123

                    tempAttachments=[{\"name\":\"LocalPath\reports.docx\", \"categoryId\":0}]

I tried to put that under Body too, still got 500 Error Code

Body :-"issueKey=Test-123&tempAttachments=[{\"name\":\"LocalPath\reports.docx\", \"categoryId\":0}]"

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 8, 2019

What was the error?

issueKey=Test-123 should be issueKey=TEST-123

and value of tempAttachments should be JSON string (JSON.stringify(data))

Have you checked headers that you are passing?

Shweta August 8, 2019

I don't think I am passing anything in the header.

Please see the attached screenshot and let me know what needs to be changed/added

attachment.JPG

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 9, 2019

It not clearly visible but I don't see any headers.

Shweta August 9, 2019

I am not passing anything in the header. What needs to be passed?

 issueKey=Test-123

tempAttachments=[{\"name\":\"LocalPath\reports.docx\", \"categoryId\":0}]

I tried passing the above as header too, but it still did not work.

I really need a way to work this out for the client.

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 9, 2019

Above information need to passed as data not headers. I wanted to state that 'Test-123' should be 'TEST-123'

You can use Atlassian Rest API Browser to test API right from Jira Admin UI. Once you get hold of request there then try it from outside.

Shweta August 9, 2019 edited

When you say passing as Data - does that mean Parameter, Body or  Resource (marked in yellow)

Passing it just in the resource gave me 412 status code.

attachment_1.JPG

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 10, 2019

You need to pass data as body.

Have you tried Altassian Rest API Browser, it will easier for you to try there before using any other tool.

Shweta August 12, 2019 edited

When I try passing data as body, I get 500 error code.

For Altassian Rest API Browser, do I need any Atlassian application? Since, I don't have one, what is best approach?

0 votes
DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 6, 2019

By default Jira Attachment doesn't have any category, are you using any plugin like "Smart Attachment"?

If you are using plugin, you need to refer plugin API how to categorize attachment after adding them to an issue.

Shweta August 6, 2019

I believe so yes, the client is using Smart Attachment. The process which I am working for my client involves adding attachments to the Data Category of the issue using  Jira REST API. Is there a way I can find the ID of the Data Category and use that ID to add attachment ?

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 6, 2019

Categories of Attachment are created by `Smart Attachment` plugin so you will not be able to use it via Jira Standard Rest APIs.

But you can use Rest APIs provided by Smart Attachment plugin to assign categories to attachment.

Please look here this will solve your problem - https://docs.stiltsoft.com/display/public/CATAT/Managing+Attachments+with+REST+API

Shweta August 7, 2019

Sorry, I am very new to Jira Rest APIs.

Should I try one of these methods?

1. https://docs.stiltsoft.com/display/public/CATAT/Managing+Attachments+via+REST+API#ManagingAttachmentsviaRESTAPI-Movingattachmentsfromonecategorytoanother

I can upload the attachments to Standard Category, get all the attachment Id's and then move them to Data category 

2. Did not quite understand this method  

https://docs.stiltsoft.com/display/public/CATAT/Managing+Attachments+via+REST+API#ManagingAttachmentsviaRESTAPI-Puttingattachmentstocategoriesbeforeupload

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 7, 2019

2 seems to be a better way. Here is how you can do this,

Let say you want to attach file `reports.docx` to issue `SP-1`

Using API provided by Smart Attachment plugin,  you will first assign desired category to filename associated with issue.

curl -D- -u admin:admin --request POST "https://<jira_home>/rest/attach-cat/1.0/attachments" --data "issueKey=SP-1&tempAttachments=[{\"name\":\"reports.docx\",
 \"categoryId\":0}]"

Now you have assigned category with ID `0` to file `reports.docx` before it is uploaded to Jira.

Now you need to call standard Jira API to upload file

curl -D- -u admin:admin -X POST  -F "file=@reports.docx" http://<jira_home>/rest/api/2/issue/SP-1/attachments
Shweta August 7, 2019 edited

So, you are suggesting to make 3 API calls? I am using SOAP UI to achieve this

1. API call to get Category Id

Endpoint :- 'GET/rest/attach-cat/1.0/attachments?issueKey=TEST-123'

2. After getting the Category ID (0) from the JSON string, make an API call to assign it to the attachment

Endpoint :- 'POST/rest/attach-cat/1.0/attachments'

Body or Parameter ?  :- "issueKey=Test-123&tempAttachments=[{\"name\":\"reports.docx\", \"categoryId\":0}]"

3. API call to add attachment

Endpoint :- POST/rest/api/2/issue/Test-123/attachments

Attachment :- file:"reports.docx"

Header :- X-Atlassian-Token:"nocheck"

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 7, 2019

Yes.

reda baalou
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 1, 2024

i have the same issue in 2024 any help 

reda baalou
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 5, 2024

so i found the solution that works , in my case i did 3 api calls 

1. API call to get Category Id

Endpoint :- 'GET/rest/attach-cat/1.0/attachments?issueKey=TEST-123'

2.  API call to add attachment

Endpoint :- POST/rest/api/2/issue/Test-123/attachments

Attachment :- file:"reports.docx"

Header :- X-Atlassian-Token:"nocheck"

and i get from the response the attachemet id which i send in my 3rd api call  , for exemple 3

3. API call to move the attachement 

endpoint POST rest/attach-cat/1.0/attachments/move

 

application/x-www-form-urlencoded :  
toCatId=0 , attachments= 3 , issueKey=TEST-123

Suggest an answer

Log in or Sign up to answer