Getting issue while Integrating Jira REST API while Creating Project

Hello Support ,

Hope you are doing well !!!

I am integrating JIRA Rest api to create new project in c# language for a specific user.
Below is the code which am i implementing

Uri uri = new Uri("http://jirademoseasia.atlassian.net/rest/api/2/project");
 var credentialCache = new CredentialCache();
 credentialCache.Add(
   new Uri(uri.GetLeftPart(UriPartial.Authority)), // request url's host
  "Basic",  // authentication type. hopefully they don't change it.
    new NetworkCredential("xxxxxxxx","xxxxxxxx") // credentials 
     );
    using (WebClient client = new WebClient())
      {
    client.UseDefaultCredentials = true;
    client.Credentials = credentialCache;
     System.Collections.Specialized.NameValueCollection formParams = new        System.Collections.Specialized.NameValueCollection();
 formParams.Add("key", Test Project");
 formParams.Add("name", "Test Project");
  formParams.Add("projectTypeKey", "business");
    formParams.Add("projectTemplateKey", "com.atlassian.jira-core-project-templates:jira-core-project-management");
   formParams.Add("lead", "admin");
   formParams.Add("url", "http://abc.com/");
   formParams.Add("assigneeType", "UNASSIGNED");
   formParams.Add("avatarId", "10204");
   formParams.Add("issueSecurityScheme", "10000");
   formParams.Add("permissionScheme", "10100");
   formParams.Add("notificationScheme", "10100");
   formParams.Add("categoryId", "10000");
   client.Headers.Add("Content-Type", "application/json");
   client.Headers.Add("User-Agent", "xx");
    byte[] responsebytes = client.UploadValues("http://jirademoseasia.atlassian.net/rest/api/2/project", "POST", formParams);
  string result = System.Text.Encoding.UTF8.GetString(responsebytes);
  return result;

But in response i am getting empty string and no project is created for this particular user. Please let me know how can i implement my code to create new project.

Thanks
Harwinder Singh

From reading the documentation for WebClient.UploadValues WebClient.UploadValues I don’t believe that you can submit JSON – the content-type must be application/x-www-form-urlencoded which obviously won’t work with JIRA’s REST API. Are you certain that your code isn’t throwing a WebException?

You will need to use UploadString – see this StackOverflow question and its answers.

yes this code is not throwing any kind of exception but in response also not getting any data just an empty string

@tdavies said - this rest end point isn’t a regular atlassian end point. You have to support a form to it.

This is java code that I have that works in our automated tests:

 HttpPost request = new HttpPost(contextPath+"/rest/project-templates/1.0/templates");

            Map<String, String> data = new HashMap<String, String>();

            List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();

            urlParameters.add(new BasicNameValuePair("projectTemplateWebItemKey",webItem));
            urlParameters.add(new BasicNameValuePair("projectTemplateModuleKey",moduleKey));

            urlParameters.add(new BasicNameValuePair("name",projectName));
            urlParameters.add(new BasicNameValuePair("key",projectKey));
            urlParameters.add(new BasicNameValuePair("lead", leadUser));
            urlParameters.add(new BasicNameValuePair("keyEdited","false"));



            request.addHeader("content-type", "application/x-www-form-urlencoded");
            request.addHeader("Accept", "*/*");
            request.addHeader("Authorization", "Basic " + new String(credentials));
            request.addHeader("X-Atlassian-Token","nocheck");


            request.setEntity(new UrlEncodedFormEntity(urlParameters));

            HttpResponse response = httpClient.execute(httpHost, request);
     

Hey @daniel and @tdavies,
Thankyou for your help.

As per you said Content-Type must be “application/x-www-form-urlencoded” and i have implemented it on POSTMAN software but it is giving me error “415 Unsupported Media Type” as per screenshot below

So Please tell me if you have any suggestion for this.
I have implemented the following code now:-

 var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://jirademoseasia.atlassian.net/rest/api/2/project");
 SetBasicAuthHeader(httpWebRequest, "username", "password");
 httpWebRequest.Method = "POST";
 httpWebRequest.Headers.Add("UserAgent", "xx");
 httpWebRequest.ContentType = "application/x-www-form-urlencoded";
 using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
 {

 string strNew ="key=TEST&name=Testing Project&projectTypeKey=business&projectTemplateKey=com.atlassian.jira-core-project-templates:jira-core-project-management&description=description&lead=admin&url='http://abc.co/'&assigneeType=UNASSIGNED&avatarId=10204&issueSecurityScheme=10000&permissionScheme=10100&notificationScheme=10100&categoryId=10000";

 streamWriter.Write(strNew);
 streamWriter.Flush();
 streamWriter.Close();

 var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
 using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
   {
        result = streamReader.ReadToEnd();
    }

Hello, has anyone figured out a solution for this ? I am also getting unauthorized error and I am sure my username/pw is correct. I am trying to pass in my windows credential using BASIC authentication added to the header but no luck.