Linking inline image to attachment using API

adam_carter March 23, 2022

Within some fields in JIRA, images can be added inline such as in the description or in comments. Although they appear inline, they are seemingly being added as attachments and then JIRA is maintaining a link somewhere. Unfortunately, I'm struggling to figure out what that link is when using the API.

Calling /rest/api/3/issue/<issue-key> against an issue where an image has been added inline returns the following:

{
                    "type": "mediaSingle",
                    "attrs": {
                        "layout": "align-start"
                    },
                    "content": [
                        {
                            "type": "media",
                            "attrs": {
                                "id": "056f8363-192c-4f50-85a6-9ce2f4dca583",
                                "type": "file",
                                "collection": "",
                                "width": 522,
                                "height": 176
                            }
                        }
                    ]
                },
Unfortunately, the ID shown here is not the id that is needed to access the attachment content. That is - rest/api/3/attachment/content/38477.
The Attachment property of the issue response does show the 38477 ID but there is no reference to the 056f ID shown in the inline comment that would enable a link to be established:

"attachment": [
            {
                "self": "/rest/api/3/attachment/38477",
                "id": "38477",
                "filename": "image-20220322-152329.png",
                "author": {<removed for brevity},
                    "displayName": "<user name>",
                    "active": true,
                    "timeZone": "Europe/London",
                    "accountType": "atlassian"
                },
                "created": "2022-03-22T15:24:03.145+0000",
                "size": 25485,
                "mimeType": "image/png",
                "content": "/rest/api/3/attachment/content/38477",
                "thumbnail": "/rest/api/3/attachment/thumbnail/38477"
            }
        ],
Obviously this is fine all the time there is only a single attachment, but as soon as more than one is added then I need to see the link somehow.
At present, I'm just trying to figure out how to make that link in order to get data out. Once I'm done, then I'll switch my attention to how to add an inline image via the API which I had previously assumed was a case of adding the image as an attachment and then referencing the returned ID when attempting to create the inline image. That now seems like that is not going to work either.
Any help anybody can give would be much appreciated.

2 answers

6 votes
Sam Watkins November 7, 2022

After uploading an image attachment, you can do an HTTP request to fetch the attachment content, which will redirect to a media URL. The media ID that you need can be extracted from this media URL. You can get the media URL by doing a HTTP GET without automatically following redirects. There's no need to actually fetch the image, just look at the redirect URL (in the HTTP "Location" header).

For example, when adding an inline image to the description, you can get the media ID in this way, then update the description or comment to use that image.  I've tested this just now, and it works.

Here's an example.

If this is the main attachment URL:

https://example.atlassian.net/rest/api/3/attachment/12345

When we fetch it, the JSON contains a "content" property, which would be:

https://example.atlassian.net/rest/api/3/attachment/content/12345

When we do an HTTP GET on this content URL, it redirects to another URL using the Location header:

Location: https://api.media.atlassian.com/file/12345678-1234-1234-1234-123456789abc/binary?token=WHATEVER&client=WHATEVER&dl=true&name=something.jpg

We need that UUID "12345678-1234-1234-1234-123456789abc" that comes after /file/ in the redirected URL, this is the media ID.

You can then use that media id in the ADF, something like this:

{
"version": 1,
"type": "doc",
"content": [
{
"type": "mediaSingle",
"attrs": {
"layout": "align-start",
"width": 100.0,
},
"content": [
{
"type": "media",
"attrs": {
"id": "12345678-1234-1234-1234-123456789abc",
"type": "file",
"collection": ""
}
}
]
}
]
}

If you need to go the other way, and find the attachment ID (12345) given the media ID (12345678-1234-1234-1234-123456789abc), you could scan all the issue's attachments one by one in this way.  In our case, we are tracking these attachment IDs and media IDs along with some other information in a database, so we don't have to query every attachment each time.

Board Genius December 13, 2022

This is crazy. Thank you for figuring this out and sharing.

Like # people like this
Jehan Bhathena
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, 2023

Three cheers to @Sam Watkins .

:-)

This is a great workaround.

Lam Le August 25, 2023

Hey @Jehan Bhathena , I just tested this and wasn't able to find the location header. Are you? 

Jehan Bhathena
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 25, 2023

Hi @Lam Le ,

yes, I checked it via my browser console.

Unfortunately not sure how we can check this on a API tool like postman.

Lam Le August 25, 2023

Ah, thanks for the guidance. Found it using my browser console, but still stuck on trying to find this location header using http get through 'Automation For Jira' as a header response. 

Jenni October 25, 2023

Is there any better way to do this..? very painful.....T^T

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.
December 21, 2023

If you need a quick and dirty way to get the attachment ID when getting the contents of a rich text field such as Comments or Description, get the HTML rendered output.

You do this by adding ?expand=renderedBody to your get request for that field, which will produce some extra JSON content, like this:

"renderedBody": "<p>This comment refers to the </p><p><span class=\"image-wrap\" style=\"\"><img src=\"/rest/api/3/attachment/content/10017\" height=\"492\" width=\"708\" style=\"border: 0px solid black\" /></span></p><p> attachment with the name test.txt</p>"


See, there is the ID of the attachment (in this case '10017') at the end of the path to it.

1 vote
Roy Davis October 26, 2022

Hi adam.carter

 

Did you ever figure this out?

 

thanks

 

- Roy

Suggest an answer

Log in or Sign up to answer