问题
I am sending the POST request to the JIRA with my json data for create a project, but I am unable to create a project into JIRA, I tried to see the error from the Fiddler and I got following error. I am using the C# and created console application for it.
My JSON data which I am posting is following.
{
"fields": {
"project": {
"key": "JTL"
},
"issuetype": {
"name": "BUG"
}
}
}
Error Message is following:
{"errorMessages":[],"errors":{"issuetype":"issue type is required"}}
I am posting json data from the following code, please suggest what and where I am wrong?
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;
In above code you can see I am sending the credentials for Authorize the user and sending the data.
回答1:
Change your data like below:
string data = @"{'fields':{'project':{'key':'JTL'},'summary':'Sample issue','description':'Creating an issue via REST API','issuetype':{'name':'Bug'}}}";
回答2:
I have solved the my problem. I made small changes in my code and my code is working successfully. i changed the url.
Old Url: https://MyCompany.atlassian.net/rest/api/2/issue
new url: https://MyCompany.atlassian.net/rest/api/latest/issue
in Json i made small change, in the "issuetype" name was Bug, which is currently not available in my account, currently in my account "TASK" issuetype is available so i changed issutype name from "Bug" to "Task". Now its working successfully!, thank god it killed my lot of time. Sad :(
Thanks Abdurrahman Koken too. :) cheers!
来源:https://stackoverflow.com/questions/26624306/how-to-create-a-issue-into-jira-through-rest-api