Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to create an issue with Rest API using C#

David Hu January 10, 2013

Hi,

I am trying to create an issue in Jira through Rest API. However, it always return "(500) Internal Server Error".

  1. I tried with SOAP Api, it worked perfectly.
  2. I tried to "Get" from @"http://localhost:8080/rest/api/2/issue/createmeta", it worked fine.
  3. the JSON data string, sDate, -->

{

"project":{"ID":10000},

"IssueType":{"ID":1},

"Summary":"This issue is created by REST Api",

"Description":"This issue is created by REST Api"

}

Here is my code.

====================

JavaScriptSerializer jss = new JavaScriptSerializer();

JiraCreateIssueRequest jcir = new JiraCreateIssueRequest();

//////////////////////////////////////////////////////////////////

string sUsername = "abc";

string sPassword = "1234567";

string uri = @"http://localhost:8080/rest/api/2/issue/";

//string uri = @"http://localhost:8080/rest/api/2/issue/createmeta";

Uri address = new Uri(uri);

HttpWebRequest request;

HttpWebResponse response = null;

StreamReader sr;

string sData = null;

string returnXML = string.Empty;

if (address == null) { throw new ArgumentNullException("address"); }

jcir.project.ID = 10000;

jcir.Summary = "This issue is created by REST Api";

jcir.Description = "This issue is created by REST Api";

jcir.IssueType.ID = 1;

sData = jcir.ToString();

try

{

// Create and initialize the web request

request = WebRequest.Create(address) as HttpWebRequest;

request.Method = "POST";

request.ContentType = "application/json";

// Add the Authorization header to the web request

request.Credentials = new NetworkCredential(sUsername, sPassword);

//Write Data

if (sData != null)

{

byte[] byteData = UTF8Encoding.UTF8.GetBytes(sData);

// Set the content length in the request headers

request.ContentLength = byteData.Length;

// Write data

using (Stream postStream = request.GetRequestStream())

{

postStream.Write(byteData, 0, byteData.Length);

}

// Get response

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

{

// Get the response stream

StreamReader reader = new StreamReader(response.GetResponseStream());

string str = reader.ReadToEnd();

}

}

catch (WebException wex)

{

// This exception will be raised if the server didn't return 200 - OK

// Try to retrieve more information about the error

if (wex.Response != null)

{

using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)

{

try

{

string sError = string.Format("The server returned '{0}' with the status code {1} ({2:d}).",

errorResponse.StatusDescription, errorResponse.StatusCode,

errorResponse.StatusCode);

sr = new StreamReader(errorResponse.GetResponseStream(), Encoding.UTF8);

returnXML = sr.ReadToEnd();

}

finally

{

if (errorResponse != null) errorResponse.Close();

}

}

}

else

{

throw new Exception(wex.Message);

}

}

-----------------------------------------------------------------

public class JiraCreateIssueRequest
    {
        protected JavaScriptSerializer jss = new JavaScriptSerializer();
 
        public JiraProject project = new JiraProject();
        public string Summary { getset; }
        public string Description { getset; }
        public JiraIssueType IssueType = new JiraIssueType();
 
        public override string ToString()
        {
            return jss.Serialize(this);
        }
    }
 
    public class JiraCreateIssueResponse
    {
 
 
    }
 
    public class JiraProject
    {
        public int ID { getset; }
        //public string Key { get; set; }
    }
 
    public class JiraIssueType
    {
        public int ID { getset; }
        //public string Name { get; set; }
    }

================================================

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
David Hu January 11, 2013

Guys,

Nevermind. Thank God that I figure it out.

Keywords here in JSON string are case sensitive and should all be LOWERCASE...

have a good weekend,

David

1 vote
David Hu January 10, 2013

I updated JSON string to add the "fields" in but I still get the same error.

"The remote server returned an error: (500) Internal Server Error."

TAGS
AUG Leaders

Atlassian Community Events