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!
Community moderators have prevented the ability to post new answers.
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:
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
By the way, when I made the second API call it didn't work without the header "X-Atlassian-Token: no-check"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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>
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Not sure, what response code/message do you get? Also try changing "Content-Type: text/html" to proper image/png or something
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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":{}}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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...
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.