Issue While creating defect in JIRA using REST API .

abhishek Pandey September 1, 2014

I have created C# application to create issue in JIRA using REST API . I am sending the request with data in URI but instead of getting the response key of created issue , i am getting HTML response to create issue in JIRA .

i Am using following code .

data = @"{ ""fields"": {

""project"":

{ ""key"": ""TSI"" }

,

""summary"": ""Test Ticket"",

""description"": ""Creating an issue using project keys and issue type names using the REST API"",

""issuetype"":

{ ""name"": ""1"" }

,

""assignee"":

{ ""name"": ""sajwagner"" }

,""reporter"":

{ ""name"": ""sajwagner"" }

}

}";

string postUrl = "https://jira-int.lufthansa.com/rest/api/2/";

HttpClient client = new HttpClient();

client.BaseAddress = new System.Uri(postUrl);

byte[] cred = UTF8Encoding.UTF8.GetBytes(m_Username + ":" + m_Password);

//byte[] cred = UTF8Encoding.UTF8.GetBytes("sajwagner:Lh2013");

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));

client.DefaultRequestHeaders.Accept.Add(new 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);

var content = new StringContent(data, Encoding.UTF8, "application/json");

//HttpContent content = new ObjectContent<string>(data, jsonFormatter);

HttpResponseMessage response = client.PostAsync("/secure/CreateIssueDetails!init.jspa?pid=11080&issuetype=1&summary=Test Ticket&description=Creating an issue using project keys and issue type names using the REST API&reporter=sajwagner&assignee=sajwagner", content).Result;

if (response.IsSuccessStatusCode)

{ string result = response.Content.ReadAsStringAsync().Result; Console.WriteLine(result); }

5 answers

0 votes
abhishek Pandey September 1, 2014

hello Cedric ,

Issue is sorted now . It was syntax error in DATA field . It should be like ..

data = @"{""fields"":{""project"":{""key"":""TSI""},""summary"":""REST EXAMPLE"",""description"":""Creating an issue via REST API"",""issuetype"":{""name"":""Bug""},""assignee"":{""name"":""sajwagner""},""reporter"":{""name"":""sajwagner""}}}";

Now issue is being created by my application . :):)
0 votes
abhishek Pandey September 1, 2014

CEDRIC ZABEL
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 1, 2014

If you keep posting your comments as answers, you’re going to confuse people. If you’re directly responding an answer, make it a comment, not a new answer.

0 votes
abhishek Pandey September 1, 2014

i am getting response

"Bad Request"

CEDRIC ZABEL
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 1, 2014

That’s just part of the response from the server. When you’re debugging client-server interactions, you should examine the entire contents of the responses. That includes the body, which in this case should tell you more about the problems you are having.

Take a look at the body of the response, and see if it tells you anything.

0 votes
abhishek Pandey September 1, 2014

If i use following code ..

data = @"{""fields"":{""project"":{""key"":""TSI""},""summary"":{""name"":""REST EXAMPLE""},""description"":{""name"":""Creating an issue via REST API""},""issuetype"":{""name"":""Bug""},""assignee"":{""name"":""sajwagner""},""reporter"":{""name"":""sajwagner""}}}";

string postUrl = "https://jira-int.lufthansa.com/rest/api/2/";

HttpClient client = new HttpClient();

client.BaseAddress = new System.Uri(postUrl);

byte[] cred = UTF8Encoding.UTF8.GetBytes(m_Username + ":" + m_Password);

//byte[] cred = UTF8Encoding.UTF8.GetBytes("sajwagner:Lh2013");

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));

client.DefaultRequestHeaders.Accept.Add(new 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);

var content = new StringContent(data, Encoding.UTF8, "application/json");

//var content = new StringContent(data);

//HttpContent content = new ObjectContent<string>(data, jsonFormatter);

//HttpResponseMessage response = client.PostAsync("secure/CreateIssue.jspa?pid=11080&issuetype=1&summary=Test Ticket&description=Creating an issue using project keys and issue type names using the REST API&reporter=sajwagner&assignee=sajwagner", content).Result;

HttpResponseMessage response = client.PostAsync("issue", content).Result;

// HttpResponseMessage response = client.PostAsync("/rest/api/2/issue/createmeta", content).Result;

if (response.IsSuccessStatusCode)

{

string result = response.Content.ReadAsStringAsync().Result;

i am getting error too

CEDRIC ZABEL
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 1, 2014

Well, what error are you getting?

0 votes
CEDRIC ZABEL
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 1, 2014

No, you’re confused here. You’re mixing up URLs for the REST API and for the human-visible HTML user interface.

The “/secure/CreateIssueDetails!init.jspa” comes from an URL used if you are creating an issue doing the pointy-clicky thing from your web browser.

If you want to use the REST API to create an issue, you need to POST data (formatted as a JSON-string) to “/rest/api/2/issue”. You wouldn’t encode data in as parameters in the URL.

Suggest an answer

Log in or Sign up to answer