Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
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

How To Upload a Project Avatar in Cloud API?

Edited


Please do not point me to the API documentation.  Documentation around the avatar load operation is TERRIBLE and the examples don't actually show how to do it.

I have a process to create projects on the fly, and in that process I'm creating PNG images, again on the fly.  I want to load these images into the cloud system so that I can set these images as avatars for my newly created projects.

Does anyone have any examples or pointers?  I can either point the load operation to a remote url, or load the url into memory and feed it uuencoded/base64'ed somehow.  I just need to understand the semantics better than what is explained at https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-avatars/#api-rest-api-3-project-projectidorkey-avatar2-post

Given that this is Jira Cloud, I sure as hell can't point to a "local file location" as it states in the documentation.

Once again this is an operation that used to be easy under Jira Server, grumble grumble.

1 answer

1 accepted

3 votes
Answer accepted

 

Okay, I figured it out.  For the next person who needs this, here's how to do it:

// Given that you have a URL to your image somewhere.  I'm using cloudconvert as a service.

def result = "<<url to your png or jpeg image >>"

// where "MT" below is the "empty" project I'm testing this out on

// Step 1.  First thing is, you need your raw bytes of your image.  Here's how to do that in Java/Groovy (ala scriptrunner):

java.net.URL url = new java.net.URL(result)
java.io.InputStream stream = url.openStream()
java.io.ByteArrayOutputStream baos = new ByteArrayOutputStream()
byte [ ] buf = new byte [ 4096 ] // the size here is arbitrary
for ( int read = stream.read ( buf ) ; read != -1 ; read = stream.read ( buf ) ) {
    baos.write ( buf, 0, read )
}
baos.close()
stream.close()
buf = baos.toByteArray()

// Step 2.  Here's the part that's documented so badly in the Jira REST API.  I don't know why the examples given include weird stuff about json headers and local paths.  It's not.  Substitute your image content type for image/png if that's not what you're using.  The "buf" is the direct, non-base64 bytes of the image,  not a path to a "local" filesystem.

def res = post ("/rest/api/3/project/MT/avatar2")
.header("X-Atlassian-Token", "no-check")
.header("Content-Type", "image/png")
.body(buf)
.asObject(Object)
.body

// Step 3, finally the easy part.  The avatar is uploaded and associated with your project, but is not selected as the actual project avatar.  Do that now.

put("/rest/api/3/project/MT/avatar")
.header('Content-Type', 'application/json')
.body([ id: res.id]) // the result from step 2
.asObject(Object)
.body

i am trying to solve the same problem. when i am taking your recommended approach (in node.js or from browser) than i am obtaining http 404 "XSRF check failed" in the post part (step 2)

did you come over this issue??

..and i agree - documentation is not just insufficient, it is wrong.

Hi Florian,

I never hit this (on this project anyway) but what you're facing is a cross-site request forgery check.  I think you're hitting this (maybe) because you're running in an external system instead of where I was doing it, inside the aegis of a running cloud system, which added the headers needed to get past this.

I found a handy fix though, for your situation.  Apparently you can just ask nicely, by adding a header:

X-Atlassian-Token: no-check

For more info, check out https://confluence.atlassian.com/cloudkb/xsrf-check-failed-when-calling-cloud-apis-826874382.html

I hope this helps!

 

 - Bill

Like Florian Bauer likes this

thank you bill - saved my live ;)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
Community showcase
Published in Jira Software

👋 Looking for 15-20 volunteers to test Atlassian training content

Hi everyone! Are you interested in beta testing Atlassian University’s newest (unreleased!) training course? We’re looking for 15-20 volunteers to test our newest training course, Basic reporting...

863 views 25 28
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