我想使用C#使用Jira Rest API创建问题
string data = @"{ ""fields"": {
""project"":
{
""key"": ""TOTEM""
},
""summary"": ""just a test"",
""description"": ""Creating of an issue using project keys and issue type names using the REST API"",
""issuetype"": {
""name"": ""Task""
},
""assignee"": { ""name"": ""imane.elbarchi"" }
}
}";
//Console.WriteLine(data);
string uri = "https://proactioneu.ent.cgi.com/rest/api/latest/issue";
System.Net.Http.HttpClient client = new HttpClient();
//Putting URI in client base address.
client.BaseAddress = new Uri(uri);
//Putting the credentials as bytes.
byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
//Putting credentials in Authorization headers.
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
//Putting content-type into the Header.
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
//I am using StringContent because I am creating console application, no any serialize I used for manipulate the string.
var content = new StringContent(data, Encoding.UTF8, "application/json");
System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;
Console.WriteLine(response);
Console.ReadKey();
}
}
}
并且我得到类似的答复:
StatusCode:200,ReasonPhrase:“ Found”,版本:1.0,内容:System.Net.Http.HttpConnection + HttpConnectionResponseContent,标头:{缓存控制:无缓存连接方式:关闭内容类型:text / html}
但未创建问题。
System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;
当我创建问题时,我使用:
var response = await httpClient.PostAsync(httpClient.BaseAddress, content);
因此第一个参数需要您要将内容发送到的网址。 “问题”不是网址。我的代码看起来像这样。也许您可以使用它。
public async Task<bool> PostIssueAsync(string userpass, string data) { HttpClient httpClient = new HttpClient(); httpClient.BaseAddress = new Uri(Constants.JiraUrl + "rest/api/latest/issue"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", userpass); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var content = new StringContent(data, Encoding.UTF8, "application/json"); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); try { var response = await httpClient.PostAsync(httpClient.BaseAddress, content); return response.IsSuccessStatusCode; } catch (Exception ex) { Console.WriteLine(ex); return false; } }