Issue While creating defect in JIRA using REST API .

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

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.

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

Well, what error are you getting?

i am getting response

"Bad Request"

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.

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.

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 . :):)

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,975 views 12 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot