Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Please Help! Got error 400 when trying to add watcher using REST API.

Johanes_Sungarda June 12, 2012

Dear all,

I'm trying to add 'watcher' support from our iPhone app that access JIRA using REST API.

I try to invoke "/rest/api/2/issue/<key>/watchers" and always get "400 bad request" error message! I use http method "POST" with data containing user name requesting to watch. In JIRA REST API doc, it say:

acceptable request representations:

  • application/json<abbr title="{http://research.sun.com/wadl/2006/10} "></abbr> [collapse]

    <h6>Example</h6>
    "fred"

I try to send only "fred" string, json array "fred", dictionary "name=fred", but NOTHING works!

DELETE request works perfectly.

Please help as we want to release update for our iPhone app ("Resolveit for JIRA") as soon as possible.

Best regards,

Joe

11 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

5 votes
Answer accepted
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 12, 2012

Try sending the data with double quotes. Not just "fred" string but "\"fred\"". The following curl worked for me:

curl -i -u admin:admin -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '"jobinkk"' http://localhost:8080/rest/api/2/issue/TEST-1/watchers

Johanes_Sungarda June 13, 2012

Hi Jobin, That works perfectly :). Thanks a lot.

SRIDHAR October 25, 2012

curl -D- -u admin:admin -X POST --data @C:\Users\folder\Desktop\testRest.json -H "Content-Type: application/json" http://jirainstanceon8w7:2990/rest/api/2/issue

what is wrong with my command ? Am i missing some parameter in it to create an issue

cglllcglll September 10, 2018

Hi  I am using postman to test this REST command I have tried with  quotes, without quotes, with keys but nothing seems to work.problem.png

1 vote
Pavan Reddy March 17, 2015

Hope this helps someone as it was painful to determine what works

 

curl -i -u admin:admin -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d """jobinkk""" http://localhost:8080/rest/api/2/issue/TEST-1/watchers

1 vote
Andrew Fisher August 27, 2013

Does anyone know if it's possible to add a list of names to the watchers list in a single call, or would it have to be a separate call for each user to be added? (I tried a couple of different ways of passing in a list of names to the API, but it only ever took the first name).

0 votes
mayank chopra June 1, 2017

This is bug since very long in JIRA .

 

https://jira.atlassian.com/browse/JRASERVER-29304

 

0 votes
RuijunFan May 26, 2016

I tried to add a watcher to one JIRA issue, but anyway,  it returns "bad request".

I can update assignee, comment, labels, .... with the similar code below, only watcher got me.

Thanks in advance.

my code is as below:

public void updateJiraWatcher(string key,string watcherName)

 

var request = new RestRequest()

{

Method=Method.Post,

Resource = "/rest/api/2/issue/"+key+"/watchers",

RequestFormat= DatFormat.JSon

};

string jsonWatcher ="{\""+watcherName+"\"}";  // "{\"username\":\""+watcherName +"\"}; ** doesn't work too

request.AddParameter("application/json",jsonWatcher,ParameterType.RequestBody);

var response = GetRequestsResponse(request); 

0 votes
RuijunFan May 26, 2016

Hello

I have the same problem, always got a bad request ,statuscode :400,

the above answers don't help,

my code below:

I tried to add a watcher to one JIRA issue, but anyway,  it returns "bad request".

I can update assignee, comment, labels, .... with the similar code below, only watcher got me.

Thanks in advance.

my code is as below:

public void updateJiraWatcher(string key,string watcherName)

 

var request = new RestRequest()

{

Method=Method.Post,

Resource = "/rest/api/2/issue/"+key+"/watchers",

RequestFormat= DatFormat.JSon

};

string jsonWatcher ="{\""+watcherName+"\"}";  // "{\"username\":\""+watcherName +"\"}; ** doesn't work too

request.AddParameter("application/json",jsonWatcher,ParameterType.RequestBody);

var response = GetRequestsResponse(request); 

0 votes
TraollyX October 18, 2013

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

0 votes
TraollyX October 16, 2013

Hello,

I am also getting a 400 error. I'm doing this with java and am curiuos if anyone got the json syntax to work with the JRest APIs via Jersey classes?

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

0 votes
Mauricio Leyzaola July 17, 2013

Just my $0.02 here for someone who is using .NET to post. Make sure you enclose your string between quotes:

string.Format("\"{0}\"",userName)

Otherwise it was failing in my env since we are using firstname.lastname for the usernames and that period in the middle was the issue.

0 votes
fanluo March 29, 2013

Use JSONObject.quote function pass a string. This worked for me in REST API

0 votes
Lee Correll
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 7, 2013

Is this still valid? I'm getting the 400/Bad Request error using

a _curlrc file with

-H "Content-Type: application/json"

-k

-u username:password

--silent

--show-error

and a command line of

./curl -i -H "Accept: application/json" -X POST -d '"username"' https://hostname.jira.com/rest/api/latest/issue/ISSUE-id/watchers

"The request sent by the client was syntactically incorrect (Bad Request)"

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events