Unable to PUT comment to Jira with Javascript using REST API

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
Accepted answer
Tom Davies Atlassian Team Jun 04, 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

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
Community showcase
Posted Tuesday in United States

Administrators Cleanup

Good morning, After a long weekend cleaning up, I am still in the cleanup mode 😊. I am curious to know: When do you feel the urgency to cleanup your Atlassian Tools? How many times do y...

56 views 1 2
View post

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you