Hi everyone,
I got 405 method not allowed when create issue with REST API in C#. it worked in cURL command.
curl -D- -u myuserid:mypassword -X POST --data "{\"fields\":{\"project\":{\"key\":\"CQT\"}, \"summary\":\"Sample issue\",\"description\":\"Creating an issue via REST API\",\"issuetype\": {\"name\":\"Bug\"}}}" -H "Content-Type: application/json" http://myjiraserver.com:8080/rest/api/2/issue/
here is my code, can someone help? I had been stuck on this for a week,
public string RunQuery_CreatIssues (
JiraResource resource,
string argument = null,
string data = null,
string method = "POST")
{
const string m_BaseUrl = "http://myjiraserver:8080/rest/api/2/issue/";
string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString());
if (argument != null)
{
url = string.Format("{0}{1}/", url, argument);
}
data = "{\"fields\":{\"project\":{\"key\":\"CQT\"},\"summary\":\"Sample issue\",\"description\":\"Creating an issue via REST API\",\"issuetype\":{\"name\":\"Bug\"}}}";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] databyte = encoding.GetBytes(data);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/json";
request.Method = method;
request.ContentLength = databyte.Length;
string base64Credentials = GetEncodedCredentials();
request.Headers.Add("Authorization", "Basic " + base64Credentials);
if (databyte != null)
{
Stream newStream = request.GetRequestStream();
newStream.Write(databyte, 0, databyte.Length);
newStream.Close();
}
string result = string.Empty;
try
{
HttpWebResponse webresponse = (HttpWebResponse)request.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
StreamReader responseStream = new StreamReader(webresponse.GetResponseStream(), enc);
result = responseStream.ReadToEnd();
responseStream.Close();
webresponse.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return result;
}
Community moderators have prevented the ability to post new answers.
Volodymyr,
I don't know how to express my appreciation for your answer. right on the spot.
because I have defined
public enum JiraResource
{ project, issue }
and use JiraResource Enum as input param.
string issueString = RunQuery_CreatIssues(JiraResource.issue);
string issueString = RunQuery_CreatIssues(JiraResource.project);
I have to change my baseurl to
const string m_BaseUrl = "http://eaicncadev030:8080/rest/api/2/";
and bingo.
> I don't know how to express my appreciation for your answer Just accept/upvote it :-)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Why do you concatenate "http://myjiraserver:8080/rest/api/2/issue/" with resource:
string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString());
I guess the malformed string causes 405 method not allowed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Volodymyr, Do you mind if I can ask you another question? In the Create Issue JSON object, how do I get all the required fields for the JIRA issue? for example: string data = "{\"fields\":{\"project\":{\"key\":\"CQT\"},\"summary\":\"Sample issue\",\"description\":\"Creating an issue via REST API\",\"issuetype\":{\"name\":\"Bug\"}}}"; I can tell, project, summary, description, issuetype are the fields for Issue. How do I get the rest of fields of Jira Issue?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Andy, Best practice is to ask new questions as new questions since more people will see it and you get higher chances to get answer or get better answer. Answering your question: I guess you need to fetch /rest/api/2/issue/{issueIdOrKey} GET method to get all the details. You can use "fields" param to return only field you need. Please see the help for more details: https://docs.atlassian.com/jira/REST/latest/#d2e914
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
https://jira.domain.com/rest/api/latest/issue/createmeta?projectIds=<projectId>&expand=projects.issuetypes.fields
Will get you all the required fields for all issue types given the project id you will be using. Where jira.domain.com is your specific instance and <projectId> is the id value of the project.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.