通过REST创建JIRA问题C#的HttpClient [英] Creating jira issue via Rest c# httpClient

查看:275
本文介绍了通过REST创建JIRA问题C#的HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读Atlassian的的 https://answers.atlassian.com/questions/79902/using-httpclient-c-to-create-a-jira-issue-via-rest-generates-bad - 请求 - 响应其中一个用户通过下面的代码创建一个JIRA问题。我适应,但通过使用 ObjectContent

一个自建类问题

 <$得到一个错误C $ C> Http.HttpContent内容=新Http.ObjectContent<&期GT;(数据,jsonFormatter); 



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



 公共字符串CreateJiraIssue()
{

字符串数据= @{田:{
,预测,:
{
关键:HELP,
},
摘要:测试票,
说明:,
,使用使用REST API项目的按键和问题类型的名称问题的创建,问题类型:{
,名:门票,
},
,受让人:{名:用户}
}
};
串postUrl =https://xxx.jira.com/rest/api/2/;
System.Net.Http.HttpClient客户端=新System.Net.Http.HttpClient();
client.BaseAddress =新的System.Uri(postUrl);
字节[] = CRED UTF8Encoding.UTF8.GetBytes(用户名:密码);
client.DefaultRequestHeaders.Authorization =新System.Net.Http.Headers.AuthenticationHeaderValue(基本,Convert.ToBase64String(CRED));
client.DefaultRequestHeaders.Accept.Add(新System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(应用/ JSON));
System.Net.Http.Formatting.MediaTypeFormatter jsonFormatter =新System.Net.Http.Formatting.JsonMediaTypeFormatter();

System.Net.Http.HttpContent内容=新System.Net.Http.ObjectContent<&期GT;(数据,jsonFormatter);
System.Net.Http.HttpResponseMessage响应= client.PostAsync(问题,内容)。结果;
如果(response.IsSuccessStatusCode)
{
字符串结果= response.Content.ReadAsStringAsync()结果。
返回结果;
}
,否则
{
返回response.StatusCode.ToString();
}

和使用



 命名空间IOnotification_System 
{
公共类问题
{
公共字段的字段{搞定;组; }
公开发行()
{
栏=新领域();
}
}

公共类字段
{
公共工程项目{搞定;组; }
公共字符串摘要{搞定;组; }
公共字符串描述{搞定;组; }
公共受让人受让{搞定;组; }
公共问题类型问题类型{搞定;组; }
公共字段()
{
项目=新的项目();
=问题类型的新问题类型();
}
}

公共类项目
{
公共字符串键{获得;组; }
}

公共类问题类型
{
公共字符串名称{;组; }
}
公共类受让人
{
公共字符串名称{;组; }
}
}


解决方案

修改



该消息明确指出System.Net.Http.ObjectContent()预计,其第一个参数一个发行对象。我希望有是说没有转换可以从一个字符串到一个问题之后另一条消息。



您正在传递一个字符串到期望的问题的方法目的。格式化用于将发行对象转换为JSON字符串。



您已经拥有了字符串,所以在试图将其转换没有任何意义。如果你有,你要转换为JSON字符串的问题比如你只需要格式化。您可以使用的StringContent 类,并使用其属性可添加任何头不已经设置在客户端上,例如:

  VAR内容=新的StringContent(数据); 



原始



什么是错误消息,您使用的是什么样的项目?该系统。 Net.Http.Formatting 命名空间的ASP.NET Web API的一部分。你构建一个ASP.NET应用程序,控制台应用程序,别的东西?



除非你正在构建一个ASP.NET站点此代码将无法工作。如果你唯一的问题是如何解析JSON的请求,只是用另一种反序列化的Json类。 Json.NET 是一个很受欢迎的选择。



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


I have read one answer on atlassian https://answers.atlassian.com/questions/79902/using-httpclient-c-to-create-a-jira-issue-via-rest-generates-bad-request-response where one user created a JIRA issue by the following code. I adapted it but get an error by using a self-build class issue with ObjectContent

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

The compiler wont accept it. Does anybody know why?

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

And using

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

解决方案

EDIT

The message clearly says that System.Net.Http.ObjectContent() expects an Issue object for its first parameter. I expect there is another message right after that saying that there is no conversion possible from a string to an Issue.

You are passing a string to a method that expects an Issue object. The formatter is used to convert an Issue object to a Json string.

You already have the string, so there is no point in trying to convert it. You only need the formatter if you have an Issue instance which you want to convert to a Json string. You can use the StringContent class and use its Headers property to add any headers not already set on the client, eg:

var content=new StringContent(data);

Original

What is the error message and what kind of project are you using? The System.Net.Http.Formatting namespace is part of ASP.NET Web API. Are you building an ASP.NET application, a console application, something else?

Unless you ARE building an ASP.NET site this code won't work. If your only issue is how to parse Json requests, just use another Json deserialization class. Json.NET is a very popular choice.

In any case there is no reason to use a Json class to convert a string to an HttpContent object containing that exact same string. You can use the StringContent class and use its Headers property to add any headers not already set on the client.

这篇关于通过REST创建JIRA问题C#的HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆