Tôi đã đọc một câu trả lời trên số https://answers.atlassian.com/questions/79902/using-httpclient-c-to-create-a-jira-issue-via-rest-generates-bad-request-response atlassian trong đó một người dùng đã tạo một vấn đề JIRA bằng mã sau đây. Tôi đã điều chỉnh nó nhưng gặp lỗi khi sử dụng vấn đề về lớp tự xây dựng với ObjectContent
Tạo vấn đề jira qua Rest C# httpClient
Http.HttpContent content = new Http.ObjectContent<Issue>(data, jsonFormatter);
Trình biên dịch sẽ không chấp nhận. Có ai biết tại sao không?
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();
}
Và sử dụng
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; }
}
}
Và lỗi này là? –
Tôi không thể cho bạn biết chính xác bằng tiếng Anh, nhưng nó phải là một cái gì đó như: "Sự quá tải tốt nhất cho phương pháp System.Net.Http.ObjectContent .ObjectContent (IOnotification_System.Issue, System.Net.Http .Formatting.MediaTyoeFormatter) không tương thích với danh sách đối số –
Simon
Chỉ cần sao chép và dán thư từ trình biên dịch –