【发布时间】: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