【问题标题】:C# Adding watcher to Jira isse via JSON and REST APIC# 通过 JSON 和 REST API 将观察者添加到 Jira isse
【发布时间】:2026-01-26 04:50:01
【问题描述】:

在尝试将观察程序添加到现有 Jira 问题时,我的 JSON 字符串存在语法问题,但我无法弄清楚它是什么。

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

当我通过 POST 将 JSON 提交到 URL 时,我得到 BAD REQUEST 400The request sent by the client was syntactically incorrect

我已经通过验证器运行了 JSON,结果很好。我也尝试了不同的东西,比如“用户名”而不是“名字”,更多的用户名引号,比如""""jdoe"""",当它被提交时会产生""jdoe"",等等,但似乎没有什么不同。

在程序的其他地方我可以创建和关闭 Jira 票证 - 所以我知道我的 HTTPREQUEST 和身份验证代码很好(因此我没有发布它。)

我已经研究过这个问题,并且有很多 CURL 的示例,但这对我没有多大帮助。

【问题讨论】:

  • Json 不应该是@"{""watchers"":[{""name"":""jdoe""}]}"吗?
  • 好吧,为了简洁起见,我在最初的帖子中压缩了代码。整行是这样的:string addJiraWatcher = string.Format(@"{""watchers"":[{""name"":""{0}""}]}", ConfigurationManager.AppSettings["theboss"]); 但是当我使用该字符串时,我得到了异常Input string was not in a correct format.。 :/ 我以这种方式使用的所有其他 JSON 查询都需要将大括号加倍。但是我已经将字符串输出到标签并删除了双引号和大括号 - 所以我假设这就是它在 webrequest 中提交的方式。

标签: c# json jira


【解决方案1】:

只有当你使用这样的字符串作为String.Format的参数时,你才需要加倍{{。因此,如果您只想发送常量字符串,请使用单个花括号,例如 `@"{""watchers"":[{""name"":""jdoe""}]}"。

请注意,最好使用 JSON 序列化程序来生成有效的 JSON。通常 JSON.Net 是不错的选择:

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

【讨论】:

    【解决方案2】:

    看起来这不起作用这是一个长期存在的错误,请参见此处。
    https://jira.atlassian.com/browse/JRASERVER-29304

    您将不得不更改为使用 https://yourInstance.com/rest/api/2/issue/{IssueId}/watchers

    数据将是您的经过验证的用户,用双引号括起来 """myuser.lastname"""

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

    这与大多数其他请求不同,让我发疯了几个小时。我正在使用.net,我必须用引号格式化用户名并将其传递到包装 HTTPWebRequest 的方法中。我使用一种简单的方法将数据推送到 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;
    }
    

    【讨论】:

      【解决方案3】:

      试试:

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

      这个请求的 JSON 格式有点奇怪。为了使用此 API 请求添加观察者JSON 正文需要只是 Jira 用户名作为带引号的字符串

      您可以在the API's documentation 中查看此规范。 Add watcher Jira API specification circled

      我最近在尝试使用 C# 脚本通过 Jira API 添加观察者时不得不解决这个小难题。下面是我最终在脚本中使用的函数 ->

      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);
          }
      

      【讨论】: