How can I add an image in a comment?

Julien Cambier
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!
November 30, 2018

Hi and thank you for taking the time to read my question!

I want to add a comment to an issue through the API.

I can successfully add a comment to an issue and I can also successfully add an attachment to the issue. The problem is that I want to add an image IN a comment. On the Jira Website you can drop an image or add one through an explorer, so it is possible.

Capture.PNG

When I query an issue with an image as comment I see it as:

"content": [
{
"type": "mediaSingle",
"attrs": {
"layout": "center"
},
"content": [
{
"type": "media",
"attrs": {
"id": "4f190z82-945d-4be6-cb21-9a815fb7f1v",
"type": "file",
"collection": "",
"width": 200,
"height": 183
}
}
]
}
]

 

I thought of adding a comment with this structure, but I'd need the ID to link the attachment. The ID of the comment attachment is super long ("4f190z82-945d-4be6-cb21-9a815fb7f1v") while the ID of the uploaded attachment is much shorter (23575).

Is there something I'm missing, or is adding an image as comment not possible through the API?

4 answers

10 votes
Михаил Власенко
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!
January 21, 2022

Insert it like external url

version: 1, type: 'doc', content: [ { "type": "mediaSingle", "content": [ { "type": "media", "attrs": { "type": "external", "url": "https://yordomain.atlassian.net/rest/api/2/attachment/content/10013", "width": 710, "height": 163, } } ] } ]

works perfect

Fabio Silva March 8, 2022

You are a real king. At least I don't need use any media api crazyness!

Like # people like this
Oleksandr Panov
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!
August 9, 2022

thank you, and even огромное спасибо

David Lee
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!
May 21, 2024

Thank you!! 

Like David_Bakkers likes this
4 votes
Mark Ahnell February 21, 2021

This one took me a while to figure out with a lot of trial-and-error, so I feel I should share to save the world some time. 

I am using Jira Cloud, so all the documentation pushes you to V3 API, which uses Atlassian Document Format.  There is a section for embedding attachments under Node - media, however the documentation says you need to pass in an ID which you can get by "...querying the media services API to retrieve metadata, such as, filename. Consumers of the document should always fetch fresh metadata using the Media API." I have found no documentation on a "Media API", so I gave up on V3.

However, you can use the V2 Add Comment API in Jira Cloud. 

First you call the Add Attachment API.

POST /rest/api/2/issue/{issueIdOrKey}/attachments

Response (trimmed for brevity)

{
"self":"https://your-domain.atlassian.net/rest/api/2/attachments/12345",
"id":"12345",
"filename":"image-20210221-170959.png",
"content":"https://your-company.atlassian.net/secure/attachments/12345/picture.jpg",
"thumbnail":"https://your-company.atlassian.net/secure/thumbnail/12345/picture.jpg"
}

 

Using the "content" or "thumbnail" value, call the V2 Add Comment API and surround the value with ! on either end:

POST /rest/api/2/issue/{issueIdOrKey}/comment

{
"body": "Here is my image !https://your-company.atlassian.net/secure/attachment/12345/image-20210221-170959.png!"
}

 

This also works if you are adding comments via the "External System Import":

JiraKey,Summary,Comment
TP-3,"Test API Issue","2021-02-21 03:00:00;5e5e5e5e5e5e5e5e5e5e5e5e;Here is my image !https://your-company.atlassian.net/secure/attachment/12345/image-20210221-170959.png!"
1 vote
Laurent Houdard
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!
September 22, 2021

Workaround using only V3 API:

  • add attachment
  • get attachment data
  • get "content" url without following redirections. The response will be a redirection, and the location contains the media ID.

PHP sample code:

$r1 = \Unirest\Request::get('/attachment/'.$attachment->id, [
'Accept' => 'application/json'
]);

\Unirest\Request::curlOpt(CURLOPT_FOLLOWLOCATION, false);
$r2 = \Unirest\Request::get($r1->body->content);
\Unirest\Request::curlOpt(CURLOPT_FOLLOWLOCATION, true);

if ($r2->code === 302 && isset($r2->headers['location']) &&
preg_match('|/file/([^/]+)/|', $r2->headers['location'], $matches)) {
$mediaId = $matches[1];
}
Andy Clapham
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!
July 21, 2022

Thanks Laurent - that's rather painful :)

1 vote
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 3, 2018

You can attach a file or image via the REST API in Jira. The thing to understand about doing this though is that it is not the exact same REST endpoint used to post a comment to an issue in Jira.  I understand that since you can drag/drop an image in the comment box of Jira's web UI the appearance might be that you can do this in a single go.  But when you do that, the image is actually being uploaded to the issue before you actually submit your comment. (You can test this by dragging an image file into a comment in Jira, and then not actually submitting your comment by then pressing 'Cancel', the image will still remain attached to the Jira issue).

So there are actually two different steps happening here, even in the web UI.   In your case, I believe you're going to need to call two separate rest endpoints to make this work as well.   Also you would need to make sure that the file attachment endpoint is being called first.

The KB on How to add an attachment to a JIRA issue using REST API explains how to upload images to Jira via the REST API.

Once the file is attached to the issue in question, then the steps need to reference that image in the comment is just using the wiki markup syntax to reference the filename of that image.  So in Jira, if your attachment name was testimage.png, you can make your comment look something like this

test message here
 !testimage.png|thumbnail!
see my image above

I hope this helps.

Andy

David_Bakkers
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 18, 2020

As Andy said, you have to attach the image to the issue first and know what its ID is before you can refer to it.

Shanee Dinay
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!
May 9, 2024

This did not work for me.

I have the file attached to my Jira ticket already.

When I try the comment API with your example, it doesn't work.

I see this in my Jira ticket comment:

"!testing.txt|thumbnail! see my image"

Here is the API call:

 

curl --request POST \
--url "
https://${your_jira_domain}/rest/api/3/issue
/${JIRA_ISSUE}/comment" \
--user "${JIRA_USER}:${JIRA_TOKEN}" \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"body": {
"content": [
{
"content": [
{
"text": "image !testing.txt|thumbnail! see my image above",
"type": "text"
}
],
"type": "paragraph"
}
],
"type": "doc",
"version": 1
}
}'

 

Like David_Bakkers likes this
Mark Ahnell May 9, 2024

Make sure ${API_VERSION} = 2.

Like David_Bakkers likes this
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 9, 2024

@Shanee Dinay If you're using Jira Cloud, then the API version you are using here 2 vs 3 will make a difference.   If you're using Jira Server or Jira Data Center, then see reference in Jira Data Center REST API Reference: Issue Add comment .

For Cloud, version 2 of the API will still accept inline reference of the attached filename, but the format you have for the json data is more simplified from what you are using:

--data '{
"body": "HERE IS SOME TEXT !mario.jpg|thumbnail!"
}
}'

 

But if you're calling the v3 of that endpoint, that implies Jira Cloud is using the newer editor that supports the Atlassian Document Format.  If you are using that, then you have to reference the attachment by its id within the media service, your json data section would look something like this:

--data '{
"body": {
"content": [
{
"content": [
{
"text": "Here is some text in the comment before the attached image.",
"type": "text"
}
],
"type": "paragraph"
},
{"type":"mediaGroup","content":
[{"type":"media","attrs":
{"type":"file","id":"22c209b8-1e00-434d-b1e9-48bf2b75e4c6","collection":""}
}]}
],
"type": "doc",
"version": 1
}
}'

Either way can work for Jira Cloud to reference existing attachments, but the different versions of this endpoint are expected different json inputs.

 

Like David_Bakkers likes this
Shanee Dinay
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!
May 9, 2024

We are using Jira cloud.

I updated my previous comment to show that I am using API v3.

I was already able to get it working with the v2 API format. But I wanted to try to use v3 (in case support for v2 goes away)

For the v3 API, I tried using the mediaGroup but I don't know how to get the long "id" that you have. My attachment IDs from the attachment APIs are short (5 digits long). The Jira API documentation references the Media API but through searching and also looking through comments from other threads like these, I see people saying that the Media API is not public.

I tried passing in the short attachment ID but I get an error saying my body is invalid.

Like David_Bakkers likes this
Mark Ahnell May 9, 2024

Check out my comment above from February 21, 2021.  I ran into the same exact issue, which is why I ended up using the V2 API call.  I gave up on V3 for the same reason, had no idea how to get that ID, and there is no documentation on how to use this "Media API" (at least as of February 21, 2021). 

Like # people like this

Suggest an answer

Log in or Sign up to answer