0
votes

I'm trying to create an issue in my JIRA Server instance using the REST API via C#.

I can retrieve the data fine using REST, but when I try and create I get a 405 error with the Reason Phrase 405 as well.

I've tried the solutions I've found on Google so far, including using https rather than http and I can confirm that my credentials are correct.

I'm really stuck so any help would be great!

Docs: https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/

   public string CreateJiraIssue()
    {

        string data = @"{ ""fields"": { 
                                        ""project"":
                                           {
                                               ""key"": ""TESTREST""
                                           },
                                        ""summary"": ""Test Ticket"",
                                        ""description"": ""Creating of an issue using project keys and issue type names using the REST API"",
                                        ""issuetype"": {""name"": ""Task""}
                                        }
                        }";

        string postUrl = "https://url.com/rest/api/2/issue/createmeta";
        System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
        client.BaseAddress = new System.Uri(postUrl);
        byte[] cred = UTF8Encoding.UTF8.GetBytes("username:pwd");
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

        var content = new StringContent(data, Encoding.UTF8, "application/json");
        System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;
        if (response.IsSuccessStatusCode)
        {
            string result = response.Content.ReadAsStringAsync().Result;
            return result;
        }
        else
        {
            return response.StatusCode.ToString();
        }
    }
}
1
I think the URL should be https://url.com/rest/api/2/issue (without "/createmeta") as described in the section "Creating an issue using a project key and field names"Xerillio
@Xerillio that was it!!! I'd gone blind! If you want to put it as an answer I can mark it as correct. Thank you!!!!hlh3406

1 Answers

1
votes

Looking at your code, here is what I've found:

  • Change the postUrl variable value to https://url.com/rest/api/2/issue.
  • Change the requestUri parameter in the client.PostAsync call to createmeta.