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 :(


    CommentAdd your comment...

    2 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:

      • 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:

      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.

      1. Joseph Clark

        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.

      2. Sameer Mehra

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

      CommentAdd your comment...

      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

      1. Andrzej Pasterczyk

        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" />

      2. Martin Stiborsky

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

      3. Martin Stiborsky

        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.

      4. Martin Stiborsky

        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

      5. Andrzej Pasterczyk

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

      6. Martin Stiborsky

        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.

      7. Martin Stiborsky

        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

      8. Andrzej Pasterczyk

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

      CommentAdd your comment...