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:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @Jehan Bhathena , I just tested this and wasn't able to find the location header. Are you?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@David Bakkers this is not the same id that is being discussed. The ID that you refer to here is the attachment id. There is another ID that is the media services ID (a longer UUID-like ID that is used for embedding images directly within content).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi adam.carter
Did you ever figure this out?
thanks
- Roy
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.