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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Linking inline image to attachment using API

Edited

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

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.

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.
Aug 06, 2023

Three cheers to @Sam Watkins .

:-)

This is a great workaround.

Lam Le
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!
Aug 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.
Aug 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
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!
Aug 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. 

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

Hi adam.carter

 

Did you ever figure this out?

 

thanks

 

- Roy

Suggest an answer

Log in or Sign up to answer