How to attach a text file to a comment to jira ticket using REST API?

dmytrob November 4, 2019

Hi, I would like to attach a text file to a comment in Jira issue via REST API.
I see that it is possible over web interface, so I assume it is also possible via REST API.

1 answer

1 vote
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 12, 2019

Hi @dmytrob 

I understand that you are looking for a way to attach a text file to a comment in Jira using the REST API.  This can be done, but it technically requires two different REST API POST commands to two different REST endpoints to make this work.

In order for you to be able to link to an attachment in the comments or description of a Jira issue, that attachment has an expectation that it will have already been uploaded to that specific Jira issue.  Once that is true, you can then reference that attachment via the wiki-markup of the comment itself.

Check out the Jira Server REST API reference for POST /rest/api/2/issue/{issueIdOrKey}/attachments.  That explains how you can add an attachment to a Jira issue first.  Once that command has completed, you can then add a comment using the endpoint POST /rest/api/2/issue/{issueIdOrKey}/comment.  If the filename that is uploaded to that Jira issue is called example.txt then the body of that comment should contain some wiki-markup like this:

Here is a link to the [^example.txt] file.

That should then permit your comment to have a link directly to that attachment on that issue.

Let me know if you have any questions about this.

Cheers,

Andy

Mohamed Arfaoui January 15, 2022

Hello @Andy Heinzer 
is it possible now to post a comment with attachment directly?

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 18, 2022

The REST API in Jira can perform both of those functions. However they are still required to happen within two separate REST API calls.  Users have sought to have a single REST API Post call that can add a new attachment, and then perform some other action, such as creating an issue or adding a comment to an existing issue. 

While that is possible to do within the web interface, the REST API is limited in this regard.  There are feature requests for this behavior over in

jomo October 24, 2023

Hey @Andy Heinzer,

the approach you are mentioning only works when entering the string directly as a comment. When sending it via an API post request, jira transforms 

!logo.png|!    

to

\!logo.png\|\!

 

How do I avoid these atomatic backslashes?

 

Regards

Johannes

Suggest an answer

Log in or Sign up to answer