It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to add labels to Jira via Rest API Edited

I would like to be able to add labels to a Jira using rest apis. I am using javascript currently to create an issue. I do not know the json required to create the label and need help figuring that out. I use only javascript and json for this thank you! When using postman there is a 415 error when I add "Labels":["Test"] to create a label.

 

Javascript:

var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader("Content-Type","application/json");
labels = ' {"fields": { "project": { "key": "'+ projectKey +'" }, "labels":["sd_reviewed", "ta_reviewed", "Reviewed", "Functional"]}} ';
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status == 200) {
console.log(xhr.responseText);
}
};

xhr.send(labels);

 

2 answers

Reposting cause my edit dissapeared (?)

Endpoint: https://YourJiraInstance.com/rest/api/2/issue/issue-id-right-here

Body: {"update":{"labels":[{"add":"this-is-the-label-you're-adding"}]}}

POST method, basic AUTH

0 votes
Warren Community Leader Feb 01, 2019

Hi @Devi Foskett

Within the JSON, this is the format of adding a label

 "labels": [
"Testing"
]

Hope this helps you

In postman this will return a 415 error and not work.

@Warren I have tried the code mentioned above with no avail. If you would like to see code to better understand what I am trying to accomplish I can add it above.

Warren Community Leader Feb 01, 2019

Are you trying to

  1. create an issue and set the label at the same time? If so, you're missing the issuetype. You also need to pass in values for EVERY mandatory field
  2. add a label to an existing issue?

 

What error do you get using your javascript?

Below is what my JSON looks like to create an issue and set it's label

{
"fields": {
"project": {
"key": "DS"
},
"summary": "Post Refresh work",
"issuetype": {
"name": "Task"
},
"labels": [
"Label1",
"Label2"
]
}
}

@Warren I am trying to add labels to an existing project. With the code you provided I get a status error 400.

Warren Community Leader Feb 01, 2019

If you look at this you'll see you need to do a PUT if you're updating an existing issue. Is that what you've been using?

@Warren No I have not I will try that!

I am getting returned a status error 405 now.

Like Fianna O_Brien likes this

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you