Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

SOLVED: attach a file using REST from scriptrunner Edited

Derek Fields Community Leader Jun 13, 2019

I have created a very simple test script to figure out how to attach a file to an issue. The "file" is actually just text that I want to create a file. Since this is in the Cloud, I am assuming that I can't write the file to a temp file in order to import it.

import org.apache.http.entity.ContentType;
String text = "This is a simple set of text"


def response = Unirest.post("/rest/api/3/issue/CDFT-1/attachments")
.header("Accept", "application/json")
.header("Content-Type", "text/plain")
.header("X-Atlassian-Token", "no-check")
.field("file", text.getBytes(), "text.txt")
.asJson();

I get a 415 - Unsupported Media Type error from this. Any help is welcome

2 answers

1 accepted

3 votes
Answer accepted
Derek Fields Community Leader Jun 18, 2019

After a lot of experimentation, I was able to successfully attach the file with the following code:

 

String key = 'MP-59'
def newBody = "Here is a text file that I want to attach"

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, 'test.txt')
.asJson()

 

The trick was to convert it to a stream so that it could be uploaded by Unirest 

1 vote

I've never user Unirest ...  or jira-cloud ... but had some issues like that with an actual file and I ended up with something like this using httpBuilder:

def file = new File(filePath)
def
fileEntity = new MultipartEntityBuilder()
fileEntity.addBinaryBody("file", file)

def httpBuilder = new HTTPBuilder(baseUrl)
httpBuilder.request(Method.POST, "/rest/api/latest/issue/$key/attachments"){req ->
headers.Authorization = "myBasicAuthString"
body = fileEntity.build()
}
Derek Fields Community Leader Jun 18, 2019

Note that I am not attaching an existing file, but a "file" that is simply a text string. Because I am in the Cloud, I can't create the file on disk first and then read it in as a file.

I know, I just wanted to provide the example of creating the fileEntity using the multipartEntyBuilder

btw, here is the full class path: 

import org.apache.http.entity.mime.MultipartEntityBuilder

 Looking that up a little, I see that you can do the following:

import org.apache.http.entity.mime.MultipartEntityBuilder
import groovyx.net.http.*

String text = "This is a simple set of text"

def
fileEntity = new MultipartEntityBuilder()
fileEntity.asTextBody("file", text)

def httpBuilder = new HTTPBuilder(baseUrl)
httpBuilder.request(Method.POST, "/rest/api/latest/issue/$key/attachments"){req ->
headers.Authorization = "myBasicAuthString"
body = fileEntity.build()
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Staying organized with Jira: best practices for a better project management

Project managers know this problem: A “mountain of work” lays in front of you, and you don’t know how and where to tackle them. Different to-dos lie ahead, but just one task after the other can be ha...

223 views 2 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you