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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thank you bill - saved my live ;)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.