Unable to PUT comment to Jira with Javascript using REST API

Emil G June 4, 2017

I'm trying to update a jira with a new comment through JavaScript. I can do this all day long with cURL but using javascript is proving more challenging. I was able to call the Jira API for a GET request for a key so I know my headers/authentication is working. Problem is my data. I don't see what I'm doing wrong to format the JSON string with the comment. Here's what I have so far:

$.ajax({type: "PUT",url: "https://jira.domain.com/rest/api/2/issue/TEST-113",dataType: "json",headers: { "Authorization": "Basic " + userCredentials, "Content-Type": "application/json", 'X-Atlassian-Token': 'nocheck' },data: "{\"update\":{\"comment\":[{\"add\":{\"body\": \"Test comment\"}}]}}",success: function (json) {  console.log(json)

},error: function (xhr, ajaxOptions, thrownError) {  console.log(xhr.status);  console.log(thrownError);  console.log(ajaxOptions);
}
});

I keep getting 400 Bad Request back. Plus ajaxOptions just returns "error" so I don't have any indication from Jira why it's complaining.

Thanks for any guidance.

 

1 answer

1 accepted

1 vote
Answer accepted
Tom Davies
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 4, 2017

You're creating a new comment, so you need to use the POST method. If you are updating an existing comment, then you use PUT (and supply the comment id at the end of the URL)

See https://docs.atlassian.com/jira/REST/server/#api/2/issue-addComment

Emil G June 5, 2017

Hi Tom,

Thanks for the help!  While that's not what I was doing you still lead me to the right answer eventually.

So there's 2 ways to create a comment.  The way I was trying it does use a PUT method but it uses a different JSON string hence the

{"update": { "comment" : [{"add" : {"body" : "Comment text goes here"}}]}}

I'm using that currently with cURL and it's working great.

What you showed me is a lot easier because while it uses POST, the URL adds a /comment after the jira key in the URL and the JSON I need to send is much simpler:

{"body": "Comment"}

Then it was still giving me a 400 error but after I used:

var sendComment = {"body": "Testing comment"}
sendComment = JSON.stringify(sendComment);

I was able to send the data correctly and jira was happy to add a comment.  Thanks for the help again!

Suggest an answer

Log in or Sign up to answer