2

我一直在玩 jira rest api,最终使用 4.0 .net 框架替换我在 ac# 应用程序中的 soap 实现。我也在使用restsharp。

虽然我没有遇到问题或搜索其他 api 的问题,但我一直在努力更新 jira 问题。这些是我的执行方法,它们可以很好地获取或搜索 jira 问题。我调用 SetJiraIssue 时收到的通常错误消息如下 {"errorMessages":["one of 'fields' or 'update' required"],"errors":{}}。

如果我将一个简单的 JSOn 字符串示例(字符串 jSonContent = @"{""Fields"":{""summary"":"" testing summary update""}}") 传递给请求的正文,那么它会抱怨它没有构造函数。

有人知道我做错了什么吗?欢迎提出意见和建议

  private string Execute(RestRequest request)
    {
        var client = new RestClient(_jiraUrl);

        client.Authenticator = new HttpBasicAuthenticator(_accountId, _password);
        request.AddParameter("AccountSid", _accountId, ParameterType.UrlSegment);
        var response = client.Execute(request);

        if (response.ErrorException != null)
        {
            const string message = "Error retrieving response.  Check inner details for more info.";
            var jiraManagerException = new ApplicationException(message, response.ErrorException);
            throw jiraManagerException;
        }

        return response.Content;

    }

    /// <summary>
    /// Executes a jira rest call and retuns the response if any as a business object.
    /// </summary>
    /// <typeparam name="T">Type of the return type for deserialization.</typeparam>
    /// <param name="request">THe reste request.</param>
    /// <returns></returns>
    private T Execute<T>(RestRequest request) where T : new()
    {
        var client = new RestClient(_jiraUrl);
        client.Authenticator = new HttpBasicAuthenticator(_accountId, _password);
        request.AddParameter("AccountSid", _accountId, ParameterType.UrlSegment);
        var response = client.Execute<T>(request);

        if (response.ErrorException != null)
        {
            const string message = "Error retrieving response.  Check inner details for more info.";
            var jiraManagerException = new ApplicationException(message, response.ErrorException);
            throw jiraManagerException;
        }

        return response.Data;
    }


 public void SetJiraIssue(string issueKey, JiraIssue j)
    {
        RestRequest request = new RestRequest("issue/{key}", Method.PUT);
        request.AddUrlSegment("key", issueKey);
        request.RequestFormat = DataFormat.Json;
        request.AddHeader("Content-type", "application/json");
        j.Summary = "modifiying this issue";


        request.AddBody(j);

        var response = Execute(request);
        Console.WriteLine(response);
    }
4

1 回答 1

3

我终于找到了一种让它工作的方法,这并不完全是我期望或希望它工作的方式,但这是我找到的唯一方法。希望这对其他人有用

public void SetJiraIssue(string issueKey, JiraIssue j)
    {
        RestRequest request = new RestRequest("issue/{key}", Method.PUT);
        request.AddUrlSegment("key", issueKey);
        request.RequestFormat = DataFormat.Json;

        string jSonContent = @"{""fields"":{""summary"":""test changing summary""}}";
        request.AddParameter("application/json", jSonContent, ParameterType.RequestBody);

        var response = Execute(request);
        Console.WriteLine(response);
    }
于 2013-07-16T13:52:14.883 回答