How to Create a Issue into JIRA through REST api?

Ashish-BeJovial picture Ashish-BeJovial · Oct 29, 2014 · Viewed 8.5k times · Source

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.

Answer

Abdurrahman Alp Köken picture Abdurrahman Alp Köken · Oct 29, 2014

Change your data like below:

string data = @"{'fields':{'project':{'key':'JTL'},'summary':'Sample issue','description':'Creating an issue via REST API','issuetype':{'name':'Bug'}}}";