【问题标题】:Creating jira issue via Rest c# httpClient通过 Rest c# httpClient 创建 jira 问题
【发布时间】:2012-12-20 14:22:55
【问题描述】:

我在 atlassian https://answers.atlassian.com/questions/79902/using-httpclient-c-to-create-a-jira-issue-via-rest-generates-bad-request-response 上阅读了一个答案,其中一位用户通过以下代码创建了 JIRA 问题。我对其进行了调整,但使用 ObjectContent 的自构建类问题会出错

Http.HttpContent content = new Http.ObjectContent<Issue>(data, jsonFormatter);

编译器不会接受它。有人知道为什么吗?

 public string CreateJiraIssue()
        {

            string data= @"{ ""fields"": { 
                                ""project"":
                   {
                       ""key"": ""HELP""
                   },
                                ""summary"": ""Test Ticket"",
                                ""description"": ""Creating of an issue using project keys and issue type names using the REST API"",
                                ""issuetype"": {
                                    ""name"": ""Ticket""
                                },
                                ""assignee"": { ""name"": ""user"" }
                            }
            }";
            string postUrl = "https://xxx.jira.com/rest/api/2/";
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            client.BaseAddress = new System.Uri(postUrl);
            byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            System.Net.Http.Formatting.MediaTypeFormatter jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();

            System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent<Issue>(data, jsonFormatter);
            System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;
            if (response.IsSuccessStatusCode)
            {
                string result = response.Content.ReadAsStringAsync().Result;
                return result;
            }
            else
            {
                return response.StatusCode.ToString();
            }

并使用

namespace IOnotification_System
{
    public class Issue
    {
        public Fields fields { get; set; }
        public Issue()
        {
            fields = new Fields();
        }
    }

    public class Fields
    {
        public Project project { get; set; }
        public string summary { get; set; }
        public string description { get; set; }
        public Assignee assignee { get; set; }
        public IssueType issuetype { get; set; }
        public Fields()
        {
            project = new Project();
            issuetype = new IssueType();
        }
    }

    public class Project
    {
        public string key { get; set; }
    }

    public class IssueType
    {
        public string name { get; set; }
    }
     public class Assignee
    {
        public string name { get; set; }
    }
}

【问题讨论】:

  • 我不能用英文准确地告诉你,但它一定是这样的:“System.Net.Http.ObjectContent.ObjectContent(IOnotification_System.Issue)方法的最佳重载, System.Net.Http.Formatting.MediaTyoeFormatter) 与参数列表不兼容。
  • 只需复制并粘贴来自编译器的消息
  • IOnotification_system 是 creatJiraIssue 方法和字段类的命名空间。也许错误的翻译方式不同,例如:重载方法的最佳一致性...具有无效参数。
  • Fehler 15 Die beste Übereinstimmung für die überladene System.Net.Http.ObjectContent.ObjectContent(IOnotification_System.Issue, System.Net.Http.Formatting.MediaTypeFormatter)-Methode hat einige ungültige论据。
  • 消息说明了一切。您正在将字符串传递给需要一个问题对象的方法。格式化程序用于将问题对象转换为 Json 字符串。您已经有了字符串,因此尝试转换它是没有意义的。如果您有要转换为 Json 字符串的问题实例,则只需要格式化程序

标签: c# rest jira dotnet-httpclient


【解决方案1】:

编辑

消息清楚地表明 System.Net.Http.ObjectContent() 的第一个参数需要一个 Issue 对象。我希望在那之后有另一条消息说不可能从字符串转换为问题。

您正在将字符串传递给需要一个问题对象的方法。格式化程序用于将问题对象转换为 Json 字符串。

你已经有了这个字符串,所以尝试转换它是没有意义的。如果您有要转换为 Json 字符串的问题实例,则仅需要格式化程序。您可以使用StringContent 类并使用其Headers 属性添加尚未在客户端上设置的任何标头,例如:

var content=new StringContent(data);

原创

错误信息是什么?您使用的是什么类型的项目? System.Net.Http.Formatting 命名空间是 ASP.NET Web API 的一部分。您是在构建 ASP.NET 应用程序、控制台应用程序还是其他什么?

除非您正在构建一个 ASP.NET 站点,否则此代码将不起作用。如果您唯一的问题是如何解析 Json 请求,只需使用另一个 Json 反序列化类。 Json.NET 是一个非常受欢迎的选择。

在任何情况下,都没有理由使用 Json 类将字符串转换为包含完全相同字符串的 HttpContent 对象。您可以使用StringContent 类并使用其Headers 属性添加尚未在客户端上设置的任何标头。

【讨论】:

  • Mmhh.. 不确定我是否可以 100% 关注你。我看到我有一个字符串,所以我不需要转换它。是的,你是对的,在它之后有另一条消息说不可能从字符串转换为问题。但现在我收到一个 http 错误:UnsupportedMediaType。不是说我传递的字符串有问题吗?
  • 现在可以使用了!必须添加 var content = new StringContent(data, Encoding.UTF8, "application/json");非常感谢!
【解决方案2】:

以下是神奇的:

var content = new StringContent(data, Encoding.UTF8, "application/json");

【讨论】:

    猜你喜欢
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多