简体   繁体   中英

C# Adding watcher to Jira isse via JSON and REST API

I have a syntax issue with my JSON string when attempting to add a watcher to an existing Jira issue, but I can't figure out what it is.

string URL = "http://jira/rest/api/2/issue/TS-1000/watchers"
string JSON = @"{{""watchers"":[{{""name"":""jdoe""}}]}}"

When I submit the JSON via POST to the URL I get BAD REQUEST 400 and The request sent by the client was syntactically incorrect

I've run the JSON through a validator and it came out fine. I've also tried different things like "username" instead of "name", more quotes around the username like """"jdoe"""" which will produce ""jdoe"" when it's submitted, etc but nothing seems to make a difference.

Elsewhere in the program I'm able to create and close Jira tickets - so I know my HTTPREQUEST and Authentication code is fine (hence why I didn't post it.)

I've researched the problem and there's a lot of examples for CURL, but that's not very helpful for me.

You need to double {{ only when you use such string as parameter to String.Format . So if you want to send just constant string use single curly braces like ` @"{""watchers"":[{""name"":""jdoe""}]}".

Note that it would be better to use JSON serializer to produce valid JSON. Usually JSON.Net is good choice:

 var jsonString = JsonConvert.SerializeObject(
         new {watchers = new []{new {name = "jdoe"}}})

Looks like this will not work this is a long standing bug see here.
https://jira.atlassian.com/browse/JRASERVER-29304

You will have to change to using https://yourInstance.com/rest/api/2/issue/ {IssueId}/watchers

Data would be your verified user enclosed in double quotes """myuser.lastname"""

"""myuser.lastname""" https://yourInstance.com/rest/api/2/issue/ {IssueId}/watchers

This is different than most of the other requests and drove me nuts for a few hours. I am using .net and I had to format the user name with the quotes and pass that into a method that wraps an HTTPWebRequest. I use a simple method to push the data into the request.getRequestStream.

public string RunIt(string queryString, string data = null, string method = "GET")
{
string uriString = string.Format("{0}{1}", m_BaseUrl, queryString.ToString());
Uri uri = new Uri(uriString);
HttpWebRequest request = WebRequest.Create(uriString) as HttpWebRequest;
request.ContentType = "application/json";
request.Method = method;
if (data != null)
{
    using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
    {
        writer.Write(data);
    }
}
string base64Credentials = GetEncodedCredentials();
request.Headers.Add("Authorization", Convert.ToString("Basic ") + base64Credentials);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.NoContent)
    return "204";
string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    result = reader.ReadToEnd();
}
return result;
}

Try:

string URL = "http://jira/rest/api/2/issue/TS-1000/watchers"
string JSON = @""jdoe""

The JSON format for this request is a bit weird. In order to add a watcher with this API request the JSON body needs to be just the Jira user name as a quoted string .

You can see this specification in the API's documentation . Add watcher Jira API specification circled

I recently had to figure out this little puzzle when trying to add watchers through the Jira API using a C# script. Below is the function I ended up using inside my script ->

public static void assignWatcher(string issueKey, string watcher, JiraServerInfo JInfo)
    {
        var client = new RestClient(JInfo.JServerName + "rest/api/2/issue/" + issueKey + "/watchers");
        var request = new RestRequest(Method.POST);
        request.AddHeader("content-type", "application/json");
        request.AddHeader("Authorization", "Basic " + System.Convert.ToBase64String(Encoding.UTF8.GetBytes(JInfo.JUsername + ":" + JInfo.JPassword)));
        request.AddParameter("application/json", "\"" + watcher + "\"\n", ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM