add watcher json syntax used for jira rest api (java jersey classes)

Hello Community,

I'm trying to add a watcher to an existing jira issue with java jersey classes (JIRA Rest api).

I need to pass in the json syntax to get it work, but it keeps returning a 400. Any help on what the correct json syntax to use?

ex)

//**************************************************************

// Add watcher to an existing JIRA issue

//**************************************************************

public void add_jira_Watcher(String issue_ID, String watcher) {

try {

String ji_rest_edit_URL = "http://"+ji_Host+"/rest/api/2/issue/"+issue_ID+"/watchers";

final String json_add_Watcher = "{\"username\": \"+watcher+\"}";

System.out.println("URL: " + ji_rest_edit_URL);

WebResource webResource = client.resource(ji_rest_edit_URL);

ClientResponse response = webResource.header("Authorization", "Basic " + auth_base_64).type("application/json").accept("application/json").post(ClientResponse.class, json_add_Watcher);

@SuppressWarnings("deprecation")

int statusCode = response.getResponseStatus().getStatusCode();

if (statusCode != 204) {

jrest_API_Logger.info("add_jira_Watcher() --> Watcher added failed to issue "+issue_ID+": "+statusCode);

} else {

System.out.println("wachter: " + watcher + " was added to jira issue: "+issue_ID+": "+statusCode);

}

} catch (ClientHandlerException ex) {

jrest_API_Logger.info("add_jira_Watcher() ClientHandlerException ex.getMessage(): " + ex.getMessage() + ": " + issue_ID);

} catch (Exception ex) {

jrest_API_Logger.info("add_jira_Watcher() Exception ex.getMessage(): " + ex.getMessage() + ": " + issue_ID);

}

}

Thanks in advance.

TRX

3 answers

1 accepted

I found that the parameter variable needed the quotes. Code solution below:

....

public void add_jira_Watcher(String issue_ID, String watcher) {

try {

.....

ClientResponse response = webResource.header("Authorization", "Basic " + auth_base_64).type("application/json").accept("application/json").post(ClientResponse.class, "\""+watcher+"\"");

.......

}

.....

Hope this helps anyone.

TRX

change the input string something like this

final String json_add_Watcher = "{\"+watcher+\"}";
the input param should allow only name of th watcher, check here for input params
https://docs.atlassian.com/jira/REST/latest/#d2e1398

I've tried both the below and still get an 400 error for both.

final String json_add_Watcher = "{\"+watcher+\"}";

final String json_add_Watcher = "{"+watcher+"}";

Thoughts on other things I can try?

Yes I've tried that as well and still get an 400 error response.

final String json_add_Watcher = "\"trx\"";

Thoughts?

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

106 views 0 5
Read article

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