C# Method Not Allowed using POST creating issue, why?

Adam July 6, 2018

I've been trying to figure this out for several days, trying every possible combination of JSON string I could find, still will not work.  I am trying to create a new issue in my service  desk project, I keep getting:

" System.Net.WebException: The remote server returned an error: (405) Method Not Allowed.
at System.Net.HttpWebRequest.GetResponse() "

My authentication is working fine, it just seems like I can't create an issue., my guess is a malformed json string format but I have literally copied/pasted many solutions (changing the project key ofcourse) and it's not working.  What am I doing wrong?

I am using visual studio with C# with Jira Cloud instance

string stringData = @"{""fields"": {""project"":{""key"": ""KEYHERE""},""summary"": ""REST ye merry gentlemen."",""description"": ""Creating of an issue using project keys and issue type names using the REST API"",""issuetype"": {""name"": ""Ticket""}}}";


string url = @"http://HOST.atlassian.net/rest/api/2/issue";


var data = Encoding.ASCII.GetBytes(stringData); // or UTF8

WebRequest wrUrl = WebRequest.Create(url);
wrUrl.ContentType = "application/json";
wrUrl.Method = "POST";
wrUrl.Headers["Authorization"] = "Basic " + Convert
.ToBase64String(Encoding.ASCII.GetBytes(SharepointUsername+":"+JiraAPIString));
wrUrl.ContentLength = data.Length;

var newStream = wrUrl.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();

Stream stream = wrUrl.GetResponse().GetResponseStream();

3 answers

1 accepted

1 vote
Answer accepted
Corey Tenney October 29, 2018

I know this is probably long past when you needed it, but I wanted to post a working example:

public JiraDataAccess(string appName, string url, string username, string password)
{
_applicationName = appName;
_userName = username;
_password = password;
var settings = new JiraRestClientSettings()
{
EnableRequestTrace = true
};
jira = Jira.CreateRestClient(url, username, password, settings);

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
using (Logger logger = new Logger())
{
try
{
using (JiraDataAccess jda = new JiraDataAccess(_appName, _url, _username, _password))
{
IssuePriority ip = new IssuePriority("3");

foreach (RepeaterItem item in rpProducts.Items)
{
CheckBox bhkBox = item.FindControl("chkSelect") as CheckBox;
if(bhkBox.Checked)
{
Label lblSystemName = item.FindControl("lblSystemName") as Label;
TextBox tbComments = item.FindControl("txtComments") as TextBox;
TextBox tbMirrorUser = item.FindControl("txtMirrorUser") as TextBox;
HiddenField hidProject = item.FindControl("hidProject") as HiddenField;

string name = "Onboarding";
string summary = String.Concat("New User Access Request: ", lblSystemName.Text, " - ", "User 1");
string description = String.Concat("The following new user access request has been created: ", "System: ", lblSystemName.Text,
"", "Comments: ", tbComments.Text,
"", "Mirror User: ", tbMirrorUser.Text,
"", "Username: ", "blah",
"", "Manager Name: ", "bippity boppity boo");

string reporter = "ctenney";
Task<string> tsk = jda.CreateIssue(hidProject.Value, name, ip, summary, description, reporter);
tsk.Wait();
string caseId = tsk.Result;

}
}
}
}
catch (Exception ex)
{
logger.Error(_appName, "", "btnSubmit_Click", ex);
}
}
}


public async Task<string> CreateIssue(string project, string name, IssuePriority priority, string summary, string description, string reporter)
{
string results = "";
try
{
Issue issue = jira.CreateIssue(project);
issue.Type = name;
//issue.Priority = priority;
issue.Summary = summary;
issue.Reporter = reporter;
issue.Description = description;

await issue.SaveChangesAsync().ConfigureAwait(false);

if (issue.Key != null)
{
if (issue.Key != "")
{
results = issue.Key.Value;
}
}
}
catch (Exception ex)
{
using (Logger logger = new Logger())
{
logger.Error(_applicationName, ex);
}
return null;
}

return results;
}
VIPUL AGARWAL March 7, 2019

Hi @Corey Tenney , Its a really nice work.

 

0 votes
Metin November 24, 2020

Screw jira. It's so bad. Hardly any working example out there.

0 votes
Timothy
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.
July 6, 2018
Adam July 6, 2018

Right, but every time I look for documentation on POST for jira, all I get are curl examples, and I don't know how to use curl, nor can I figure it out.

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

Adam July 6, 2018

Look at this for example, I am getting a "405" error, and I copied this directly off the documentation in Jira


string stringData = @"{""fields"": {""project"":{""key"": ""SS""},""summary"": ""REST ye merry gentlemen."",""description"": ""Creating of an issue using project keys and issue type names using the REST API"",""issuetype"": {""name"": ""Ticket""}}}";


string url = @"http://HOST.atlassian.net/rest/api/2/issue";


var data = Encoding.ASCII.GetBytes(stringData); // or UTF8

WebRequest wrUrl = WebRequest.Create(url);
wrUrl.ContentType = "application/json";
wrUrl.Method = "POST";
wrUrl.Headers["Authorization"] = "Basic " + Convert
.ToBase64String(Encoding.ASCII.GetBytes(SharepointUsername+":"+JiraAPIString));
wrUrl.ContentLength = data.Length;

var newStream = wrUrl.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();

Stream stream = wrUrl.GetResponse().GetResponseStream();




Why isnt this working?  I am going crazy here.

Timothy
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.
July 7, 2018

Couple things to make sure.

  • That the stringData turns out into a valid JSON
  • The endpoint should be https
Adam July 7, 2018

The string data is literally copy/pasted from Jira docs and other examples I found online.

Can you stop being so cryptic and actually help me out with an example?

I've tried HTTPS and HTTP, I've literally tried everything.  Can you show me at least 1 working example?  Because I have found ZERO online and it's absolutely maddening.  I have never in my life experienced such difficulty trying to communicate with an API in my life.  I also have another experienced developer trying to work with this and he is having no luck.

Suggest an answer

Log in or Sign up to answer