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

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


1 badge earned


Participate in fun challenges

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


Gift kudos to your peers

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


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!


Sending attachments via scriptrunner to another application


Hello All,

I'm trying to get attachments from the JIRA and pass them to the payload, which will send base 64 encoded data to another application.

Here's my code till now,

import groovy.transform.BaseScript
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.util.logging.*
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.user.ApplicationUser
import java.text.SimpleDateFormat
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.Attachment

// Enable the logging
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")

/** code for getting Oauth token **//

response=get_bearer_token(****OauthRequest_url, *****OauthHeaders, *****OauthPayload) ("Response is " + response)

def ***URL="............"
def access_token=response['access_token']
def ****AppHeaders=[:]
***AppHeaders['Authorization']='Bearer '+access_token

def customFieldManager = ComponentAccessor.customFieldManager
def attachmentManager = ComponentAccessor.attachmentManager

//getting JIRA issue key for testing in script console only
def issueManager = ComponentAccessor.issueManager

def issueKey = '****'

def issue = issueManager.getIssueObject(issueKey)

def attachments = attachmentManager.getAttachments(issue)

final def jiraResourcesPath = '/..jira..path/data/attachments/'
final def project = issue.projectObject
final def projectKey = project.key

def latestAttachment = attachments.findAll {
}.sort { it.created }.last()

def fileName =
def filePath = "${jiraResourcesPath}/${projectKey}/20000/${issueKey}".toString()
def oldFile = new File("${filePath}/${fileName}")
def fileContent = new File("$oldFile").text

//Encode file to Base64 format "Encoding the file..."
def encodedFile = "$fileContent".getBytes("UTF-8").encodeBase64().toString()"Encoded File:" +encodedFile)

def PayloadJson = new JsonBuilder()
fileName oldFile
fileContent encodedFile

}"Converting Json Builder to Json")
finalBodyJson = JsonOutput.toJson(PayloadJson)"Final Json object: " + finalBodyJson)
jsonContent = new JsonSlurper().parseText(finalBodyJson)"JsonContent " + jsonContent.content)
finalJsonContent = groovy.json.JsonOutput.toJson(jsonContent.content)"JsonContent "+ finalJsonContent)

def create_req(url, headers, payload,log) {
try {
def jsonSlurper = new JsonSlurper()
conn = new URL(url).openConnection()
// Adding headers
headers.each {
// Doing a POST with the connection obtained.

def retCode = conn.getResponseCode();
respText = conn.getInputStream().getText()

if(retCode.equals(202)) {"Request received. A response will be sent asynchronously.")"The response text is " + respText)
Map parsedJson= jsonSlurper.parseText(respText)
return parsedJson
else {"Encountered an Issue" + retCode)
finally {

CreateResponse = create_req(**URL, ****AppHeaders, finalJsonContent,log)


Am I getting the attachments in the right way? is there an alternate way?

Currently i have used ".pdf" but ideally i should get all the attachments and send it across?

if I run the code i get error,

2022-12-09 12:18:59,482 ERROR [common.UserScriptEndpoint]: Script console script failed: groovy.lang.MissingMethodException: No signature of method: is applicable for argument types: (File) values: [/...../data/attachments/**/20000/**-14426/1344000] Possible solutions: wait(), abs(), any(), abs(), wait(long), and(java.lang.Number) at Script163$_run_closure3.doCall(Script163.groovy:125) at Script163$_run_closure3.doCall(Script163.groovy) at

Would someone be able to assist me?


Thank You.

1 answer

Hi @Catheline ,


I am pretty sure that you are getting the attachements correctly.

Please take a look here I believe this is an answear to your question:



Suggest an answer

Log in or Sign up to answer
AUG Leaders

Atlassian Community Events