Find Questions…

Close ×
First time here? Check out the FAQ!

Avatar upload with Jira 5 REST API

Martin Stiborsky asked this question · 15 karma ·

Hello guys,

please, could you give me a example ar advice how to upload image file to Jira as avatar with REST api?

I'm lost with that :(

Thanks!

1619 views

2 Answers:

Joseph Clark [Atlassian] · 21,955 karma ·

I've been looking in to this for a day or so and I finally have a curl-based example working. :-) It's a bit convoluted. Setting a user's JIRA Avatar to a custom image is a 3 stage process via the REST API.

1) Upload the custom image as a temporary avatar

2) Convert the temporary avatar into a real avatar (this is the point at which cropping of the image is performed)

3) Set the user's avatar to be the newly created avatar from Step 2).

Important things to note:

  • You need to provide the size of the file you are uploading as a query parameter in Step 1.
  • Converting the temporary avatar into a real avatar has to be done within the same HTTP session in which you upload the temporary avatar. If you use basic authentication for each request, for example, the request to convert the avatar will fail. You'll need to configure cURL (or your client) to persist session information across calls.
  • I'm not 100% sure yet how the cropping stuff works. In my case, it was easier to just ensure that the image I uploaded was the size and dimensions I wanted, rather than trying to get JIRA to crop and scale the image on the JIRA server.

Here's my shell output for getting it to work using curl:

jclark@bluetongue /cygdrive/c/users/jclark/Pictures/temp
$ ls -l
total 8
-rwxr-xr-x+ 1 jclark None 4691 2012-04-18 16:15 joe.jpg

jclark@bluetongue /cygdrive/c/users/jclark/Pictures/temp
$ curl -u admin:admin --cookie-jar cookies.txt -X POST -H "X-Atlassian-Token: no-check" -H "Content-Type: image/jpeg" --data-binary @joe.jpg "http://bluetongue:8080/rest/api/2/user/avatar/temporary?username=jclark&filename=joe.jpg&size=4691"
{"cropperWidth":53,"cropperOffsetX":3,"cropperOffsetY":3,"url":"/secure/temporaryavatar?cropped=true&magic=1334730214402","needsCropping":true}

jclark@bluetongue /cygdrive/c/users/jclark/Pictures/temp
$ curl --cookie cookies.txt -X POST -H "Content-Type: application/json" --data-ascii '{"cropperWidth":53,"cropperOffsetX":3,"cropperOffsetY":3}' "http://bluetongue:8080/rest/api/2/user/avatar?username=jclark"
{"id":"10202","owner":"jclark","isSystemAvatar":false,"isSelected":false,"selected":false}

jclark@bluetongue /cygdrive/c/users/jclark/Pictures/temp
$ curl --cookie cookies.txt -X PUT -H "Content-Type: application/json" --data-ascii '{"id":10202,"isSystemAvatar":false,"isSelected":false}' "http://bluetongue:8080/rest/api/2/user/avatar?username=jclark"

Phew! As Martin S. mentions in his answer comments, using a web debugger tool such as Firebug or Chrome's built-in developer tools is the best way to work out how to do this, as JIRA's own in-app avatar picker uses the REST API to do its work.

Joseph Clark [Atlassian] · 21,955 karma ·

OK, I've also discovered that if the temporary avatar you upload is exactly 48x48 pixels, then the cropping step is skipped entirely and you can skip the second REST Call - instead, the initial temporary avatar upload will respond with the Avatar ID JSON that you can then use immediately to set the user's avatar permanently.

Andrzej Pasterczyk · 1,525 karma ·

Take a look at http://docs.atlassian.com/jira/REST/latest/

I guess you need to send it through /rest/api/2/user/avatar/temporary and then convert it with /rest/api/2/user/avatar

Martin Stiborsky · 15 karma ·

Yes, that I know…but, how to serialize image into JSON?

Andrzej Pasterczyk · 1,525 karma ·

You can use multipart POST request with curl same as described under /rest/api/2/issue/{issueIdOrKey}/attachments

It depends on where do you upload it from (application, HTML form)...

HTML form equivalent would look more or less like:

<form action="/rest/api/2/user/avatar/temporary?username" method="post" enctype="multipart/form-data">
<p>Select a file : <input type="file" name="uploadedFile" size="50" /></p>
<input type="submit" value="Upload It" />
</form>

Martin Stiborsky · 15 karma ·

Great, thanks for help.

I'll need perform this action from application, as reaction to Jira event for example, but also as action invoked from plugin administration, when source images are downloaded somewhere from network.

I'll start with this form example, hopefully I'll figure out how to proceed.

Martin Stiborsky · 15 karma ·

I'm trying to send file with curl, but not succesfull, any idea what is wrong?

curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: no-check" -H "Content-Type: text/html" -F "file=@/home/stibomar/useravatar.png" http://WW021376:2990/jira/rest/api/2/user/avatar/temporary?username=stibomar

Andrzej Pasterczyk · 1,525 karma ·

Not sure, what response code/message do you get? Also try changing "Content-Type: text/html" to proper image/png or something

Martin Stiborsky · 15 karma ·

ooops, sorry, I forgot to mention the response :( It is 400 / Bad request. It failed completely without text/html content type…But I can try image/png tommorow at work.

Martin Stiborsky · 15 karma ·

Here I have full response for my command (btw, doesn't matter if content-type is text/html or image/image, still same error, weird) :

curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: no-check" -H "Content-Type: text/html" -F "file=@/home/stibomar/useravatar.png" http://WW021376:2990/jira/rest/api/2/user/avatar/temporary?username=stibomar
HTTP/1.1 100 Continue

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
X-AREQUESTID: 610x3x1
Set-Cookie: JSESSIONID=79F1580F466EDF121ABF06BB6D44E905; Path=/
X-Seraph-LoginReason: OK
Set-Cookie: atlassian.xsrf.token=BP8Q-WXN6-SKX3-NB5M|a33a90b5515871cb30cdd897d3662b1ff904f904|lin; Path=/jira
X-AUSERNAME: admin
Cache-Control: no-cache, no-store, no-transform
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 01 Mar 2012 09:10:39 GMT
Connection: close

{"errorMessages":[null],"errors":{}}

Andrzej Pasterczyk · 1,525 karma ·

Honestly I have no clue at this point... you can try using other curl command line e.g.
curl -D- -u admin:admin -F "file=@file.png;type=image/png" http://WW021376:2990/jira/rest/api/2/user/avatar/temporary?username=stibomar

or maybe switch to wget or something...

Looking for something else?

Find Questions…

or Browse other questions tagged:

or Ask a Question