Avatar upload with Jira 5 REST API

Martin Stiborsky February 28, 2012

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!

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 17, 2012

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.

Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 18, 2012

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.

Sameer Mehra June 24, 2015

By the way, when I made the second API call it didn't work without the header "X-Atlassian-Token: no-check"

0 votes
Andrzej Pasterczyk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 28, 2012

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

Andrzej Pasterczyk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 28, 2012

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 February 28, 2012

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

Martin Stiborsky February 28, 2012

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 February 28, 2012

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 28, 2012

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

Martin Stiborsky February 28, 2012

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 February 29, 2012

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 29, 2012

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...

TAGS
AUG Leaders

Atlassian Community Events