Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get attachment content using REST API version 3

Danil Luchin July 21, 2022

Hi,
I'm trying to get attachment (image) content and convert it to base64 string using this endpoint  GET /rest/api/3/attachment/content/{id}
I'm getting the content as escaped ascii unicode (I guess)
and it looks like this:
����\u0000\u0010JFIF\u0000\u0001\u0002\u0000\u0000d\u0000d\u0000\u0000��\u0000\u0011Ducky\u0000\u0001\u0000\u0004....

I've been trying to unescape it, convert manually etc. – nothing worked out.
Could you please confirm that this endpoint is working correctly, as I can see Experimental tag on it?
Could you please give me a hint how to decode this binary and convert it to base64?

Also, I think it could be helpful to give an option to download the attachment as base64 string, at lease for the images.

Thank you!

3 answers

1 accepted

1 vote
Answer accepted
Prince Nyeche
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.
July 22, 2022

That content should be in bytes. You're getting binary string data. What you should do is write that content into a file data structure if you simply want to get the file data. If you want to convert to base64, I believe you could simply just convert the entire structure by decoding it to plain strings prior to using base64.

Danil Luchin July 22, 2022

Thanks, there was an issue in jira.js
now it works just fine!

Ali rahhal
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 2, 2022

Can you give me an example how did you solve it?

Ali August 3, 2022

Thank u alot. But do you have how did u recieved it in back End? Since Iam using java spring boot. And the response while accessing the jira attachment link is 403. If u have any solution for it I would be thankful.

I want to download the ImageUrl from jira attachment content using  java spring boot. 

Shyamala_Annapureddy
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 2, 2024

Hi,

We are facing a similar issue where servicenow is expecting the attachment in Base64 format and failing. Is it possible for Jira to send the attachment in Base64 format? Please suggest. 

The post that you are linking does it assume that Jira sending the attachment in Base64 format or the servicenow code in that article will convert in to Base64?

This is the API we are using: : https://api.atlassian.com/ex/jira/orgid/rest/api/3/attachment/content/id

If Jira cannot send the attachment in Base64 format we would like to get the servicenow code to convert binary (What Jira sends) to base64 format.

Thanks in advance.

Jean-François S_ Leclerc
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 15, 2024
Here's an example of how you can do it:
function fetchAttachmentAndSave(
  httpVerb,
  fullUrl,
  credentials,
  targetTable,
  targetRecord,
  filename
) {
  var request = new sn_ws.RESTMessageV2();
  request.setHttpMethod(httpVerb);
  request.setEndpoint(fullUrl);
  request.setBasicAuth(credentials.userName, credentials.password);
  request.saveResponseBodyAsAttachment(targetTable, targetRecord, filename);
  var response = request.execute();
  var httpResponseStatus = response.getStatusCode();
  var httpResponseContentType = response.getHeader("Content-Type");
  gs.info("http response status_code: " + httpResponseStatus);
  gs.info("http response content-type: " + httpResponseContentType);
}
with saveResponseBodyAsAttachment you save the binary data into an attachment... if you need its sys_id afterwards you could use 
var newAttachmentSysId = response.getResponseAttachmentSysid();
Cheers :-)
1 vote
Oscar Lopez
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 22, 2023

Hi Danil,
I have finally achieve 2 ways to resolve this.


The first is via Scriptrunner,

we can get the BinaryArray you commented "����\u0000\u0010JFIF\u0000\u0001\u0002\u0000\u0000d\u0000d\u0000\u0000��\u0000\u0011Ducky\u0000\u0001\u0000\u0004...." making use of the Attachment Content API. You can test this on the Script Console:

def issueKey = 'EXAMPLE-1234'

def result = get('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.asObject(Map)
if (result.status == 200){
def bodyfields = result.body.fields
def attachment = bodyfields.attachment[0]
def attachmentid = attachment.id
def attachmentfilename = attachment.filename
def attachmenttype = attachment.mimeType
def result2 = get('/rest/api/3/attachment/content/' + attachmentid)
.header('Content-Type', 'application/json')
.asBinary()

def attachmentcontent = result2.body.bytes.encodeBase64().toString()

 The second way is via Zapier,


You would need to use Automation for Jira and W
ebhooks by Zapier to send and capture attachment related data, such as filename, id, filetype (as you need) and in a second Zapier event you would need to use Code Python by Zapier and use this code:

import requests
import base64
import codecs
id = input_data['idattachment']

url = "https://yourdomain.attlassian.net/rest/api/3/attachment/content/"+id
r=requests.get(url , headers={"Content-Type":"application/json","Authorization":"Basic YOUR_TOKEN","Connection":"keep-alive"}, params={"redirect": "false"})attachment_bytes = r.content
base64input = codecs.encode(attachment_bytes,'base64').decode('utf-8')
attachment_text  = bytes(base64input, 'utf-8').decode('utf-8')
return {'bytes':attachment_text }

in both ways you should be able to use the base64 encripted attachment to send it to another app. 

Pablo Gómez Justicia December 19, 2023

The first vía worked for me. Thank you!!

0 votes
Shubham
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!
June 13, 2023

I am not answering .. but can you share code sniffet  how you getting attachemnt as byte[] using attachment api from java spring boot

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events