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
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 Join to answer
Community showcase
Maarten Cautreels
Posted Thursday in Off-topic

Friday Fun: What's your favourite beer/drink

As a Belgian, beer-lover and home brewer, beer is one of my great passions. I love the fact that with just a few ingredients (usually just water, hop and malt) you can create so many different tastes...

280 views 38 9
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot