Heads up! On March 5, starting at 4:30 PM Central Time, our community will be undergoing scheduled maintenance for a few hours. During this time, you will find the site temporarily inaccessible. Thanks for your patience. Read more.

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

How to send an attachment to a REST service

Cristian Yuste September 19, 2024

 

 

Hello,

I am trying to create a ScriptRunner post-function where I send the attachment in a ticket to a REST service via POST. According to the documentation of this service, I need to send the binary content of the attachment.

I have seen in the Atlassian documentation (https://docs.atlassian.com/software/jira/docs/api/9.12.2/com/atlassian/jira/issue/attachment/class-use/Attachment.html) that there is a method in the AttachmentManager class (streamAttachmentContent(Attachment attachment, InputStreamConsumer<T> consumer)) which returns the binary content of the attachment, but I don’t know how to handle the second parameter of the InputStreamConsumer. Can anyone give me a hand?

Thanks,
Best regards.

1 answer

0 votes
Sergei Troshin
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.
September 19, 2024

Hi Cristian,

You picked up rigth path! Let me provide you some java method that returns first attachment for issue ABC-1234 in bytes

import groovy.transform.Field;
import org.apache.commons.io.IOUtils;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.AttachmentManager;
import com.atlassian.jira.issue.attachment.Attachment;
import com.atlassian.jira.component.ComponentAccessor;

@Field final AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager();
@Field Issue issue = ComponentAccessor.issueManager.getIssueByCurrentKey("ABC-1234")

getAttachmentFileBytes(issue.getAttachments()?.getAt(0))

byte[] getAttachmentFileBytes(Attachment attachment) throws IOException {
return attachmentManager.streamAttachmentContent(
attachment,
inputStream -> {
try (inputStream) {
return IOUtils.toByteArray(inputStream);
}
});
}

 Hope it will help you!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.12.2
TAGS
AUG Leaders

Atlassian Community Events