Forums

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

add attachments via rest api using scriptrunner

Sandy Garrido July 22, 2019 edited

I've tried to work out with various questions already asked and the api docs.

I've not been able to work out how to post attachments to an issue using scriptrunners Groovy.

My goal is to get all the attachment URLs from one issue and add them to another issue.


def attachments = get("/rest/api/3/issue/${projectIssueKey}").asJson().body.array[0].fields.attachment

attachments.each{
def filename = it.filename
def fileUrl = it.content
def fileBody = get("${fileUrl}").asBinary().body

// POST attachment
def resp = post("/rest/api/3/issue/${newProjectIssueKey}/attachments")
resp
}

 

It's at the post attachment part that I'm not sure what to do.


1 answer

0 votes
Sandy Garrido July 22, 2019
String key = "${issueKey}"
def newBody = get("${imageURL}").asBinary().body

InputStream stream = new ByteArrayInputStream(newBody.getBytes())

def resp = Unirest.post("/rest/api/3/issue/${key}/attachments")
.header("X-Atlassian-Token", "no-check")
.field('file', stream, 'image002.png')
.asJson()
resp

 I've now got to this!
But I guess the upload is not being done correctly as there is an attachment but the size is 0.0kb

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, mindful member, mindful member badge, atlassian community, community kudos, community giveaway, atlassian swag, community values, empathy and kindness, badge challenge, atlassian learning, community engagement, swag giveaway

Earn a Badge, Win a Prize 🎁

Earning the Mindful Member badge proves you know how to lead with kindness, plus it enters you into a giveaway for exclusive Atlassian swag. Take the quiz, grab the badge, and comment on our announcement article to spread the good vibes!

Start here
AUG Leaders

Atlassian Community Events