我正在使用我的json数据向JIRA发送POST请求以创建项目,但是我无法在JIRA中创建项目,我试图从Fiddler看到错误并且我得到了以下错误。我正在使用C#并为它创建了控制台应用程序。
我发布的我的JSON数据如下。
{
"fields": {
"project": {
"key": "JTL"
},
"issuetype": {
"name": "BUG"
}
}
}
错误消息如下:
{“errorMessages”:[],“errors”:{“issuetype”:“问题类型是必需的”}}
我从以下代码发布json数据,请提出我错在哪里和哪里?
string data=@"{"fields":{"project":{"key":"JTL"},"issuetype":{"name":"BUG"}}}";
//object of HttpClient.
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("jiraUserName" + ":" + "JiraPassword");
//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");
//Sending the Post Request to the server. But getting 400 bad Request.
System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;
在上面的代码中,您可以看到我正在发送授权用户和发送数据的凭据。
更改您的数据如下:
string data = @"{'fields':{'project':{'key':'JTL'},'summary':'Sample issue','description':'Creating an issue via REST API','issuetype':{'name':'Bug'}}}";
我解决了我的问题。我在代码中做了一些小改动,我的代码运行成功。我改变了网址。
Old Url: https://MyCompany.atlassian.net/rest/api/2/issue
new url: https://MyCompany.atlassian.net/rest/api/latest/issue
在Json我做了一个小改动,在“issuetype”名称是Bug,目前我的帐户中没有,目前在我的帐户“TASK”issuetype是可用的所以我将issutype名称从“Bug”更改为“任务”。现在它成功地工作了!感谢上帝它杀了我很多时间。伤心:(
还要感谢Abdurrahman Koken。 :)干杯!