简体   繁体   English

C#通过JSON和REST API将监视程序添加到Jira isse

[英]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. 尝试将监视程序添加到现有的Jira问题时,我的JSON字符串存在语法问题,但是我无法弄清楚它是什么。

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 当我通过POST将JSON提交到URL时,我得到了BAD REQUEST 400并且The request sent by the client was syntactically incorrect

I've run the JSON through a validator and it came out fine. 我已经通过验证器运行了JSON,结果很好。 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. 我还尝试了其他方法,例如“用户名”而不是“名称”,在用户名周围加上了更多引号,例如""""jdoe"""" ,在提交时会产生""jdoe"" ,等等,但似乎没有任何作用有所不同。

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.) 在该程序的其他地方,我可以创建和关闭Jira票证-因此,我知道我的HTTPREQUEST和Authentication代码很好(因此,为什么不发布它)。

I've researched the problem and there's a lot of examples for CURL, but that's not very helpful for me. 我已经研究了这个问题,并且有很多有关CURL的示例,但这对我不是很有帮助。

You need to double {{ only when you use such string as parameter to String.Format . 仅当使用此类字符串作为String.Format参数时,才需要将{{ So if you want to send just constant string use single curly braces like ` @"{""watchers"":[{""name"":""jdoe""}]}". 因此,如果您只想发送常量字符串,请使用单个大括号,例如`@“ {”“ watchers”“:[{”“ name”“:”“ jdoe”“}]}}”。

Note that it would be better to use JSON serializer to produce valid JSON. 请注意,最好使用JSON序列化器生成有效的JSON。 Usually JSON.Net is good choice: 通常,JSON.Net是一个不错的选择:

 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 https://jira.atlassian.com/browse/JRASERVER-29304

You will have to change to using https://yourInstance.com/rest/api/2/issue/ {IssueId}/watchers 您将不得不更改为使用https://yourInstance.com/rest/api/2/issue/ {IssueId} / watchers

Data would be your verified user enclosed in double quotes """myuser.lastname""" 数据将是您的经过验证的用户,并用双引号括起来““” myuser.lastname“”“

"""myuser.lastname""" https://yourInstance.com/rest/api/2/issue/ {IssueId}/watchers “”“ 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. 我使用的是.net,我必须用引号将用户名格式化,然后将其传递给包装HTTPWebRequest的方法。 I use a simple method to push the data into the request.getRequestStream. 我使用一种简单的方法将数据推送到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. 此请求的JSON格式有点奇怪。 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 . 为了使用此API请求添加观察者, JSON主体必须只是Jira用户名(带引号的字符串)

You can see this specification in the API's documentation . 您可以在API文档中看到此规范。 Add watcher Jira API specification circled 圈出添加观察者Jira API规范

I recently had to figure out this little puzzle when trying to add watchers through the Jira API using a C# script. 最近,当我尝试使用C#脚本通过Jira API添加观察者时,我不得不弄清楚这个小难题。 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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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