A javascript rest api example for adding a JIRA comment text with a png image or just a image file in comments

1,821 views
Skip to first unread message

Neeraj Bodhe

unread,
Jul 4, 2014, 11:39:39 AM7/4/14
to atlassian-...@googlegroups.com
i am trying the following code for achieving this purpose through nodejs but my node app crashes. The same set of code when tried for attaching a file to the issue by changing it's rest url, works fine. Can anyone provide me a code snippet by which i can achieve my solution.


var httpClient = addon.httpClient({
clientKey: req.session.clientKey,
userKey: userkey,
appKey: addon.key
});

httpClient.post({
url: '/rest/api/2/issue/issue-key/comment',
headers: {
'X-Atlassian-Token': 'nocheck',

},
form: {
file: [
fileData,
{
filename: 'pngImage.png',
contentType: 'image/png'
}
]
}
}, function(err, callbackRes, body) {
res.render('display', body);
});

Seb Ruiz

unread,
Jul 6, 2014, 10:58:59 PM7/6/14
to atlassian-...@googlegroups.com
Hi Neeraj,
I do not believe you can add a comment with binary data. I think you will need to upload the image as an attachment and then reference it in the comment body.

Seb



--
You received this message because you are subscribed to the Google Groups "Atlassian Connect Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to atlassian-connec...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Seb Ruiz
Atlassian

Neeraj Bodhe

unread,
Jul 7, 2014, 8:53:32 AM7/7/14
to atlassian-...@googlegroups.com
Thanks for ur reply seb. As u described i think i wud have to use the image that is already attached to the JIRA issue, by using the markup !image.png!
At present, i am facing some silly problem with my post comment rest code, i am facing the error " errorMessages: No content to map to Object due to end of input "
Can someone tell me what exactly am i doing wrong here. Below is my code snippet.

var httpClient = addon.httpClient({
clientKey: req.session.clientKey,
userKey: userkey,
appKey: addon.key
});

httpClient.post({
url: '/rest/api/2/issue/issue-key/comment',
headers: {
'X-Atlassian-Token': 'nocheck',

'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: {
"body":"This is a my comment through rest api",
}

}, function(err, callbackRes, body) {
console.log( "body:" + JSON.stringify(body) + " callbackRes : " + JSON.stringify(callbackRes));
});

Patrick Streule

unread,
Jul 7, 2014, 2:02:39 PM7/7/14
to atlassian-...@googlegroups.com
Neeraj,

The issue seems to be that you are setting a JS object into the 'data' attribute. You would need to set the serialized JSON string there. There is an easier way, though, which also takes care of the Content-type header: Use the 'json' attribute instead of the 'data' attribute. The sample below works:

      httpClient.post(
        {
          url: '/rest/api/2/issue/TEST-1/comment',
          headers: {
            'X-Atlassian-Token': 'nocheck'
          },
          json: {
            'body': 'This comment was created through the rest api'
          }
        },
        function (err, res, body) {
          console.log(body)
        }
      );

Regards,
Patrick

Neeraj Bodhe

unread,
Jul 9, 2014, 5:38:16 AM7/9/14
to atlassian-...@googlegroups.com
Thanks peter, your suggested solution solved my problem.
Reply all
Reply to author
Forward
0 new messages