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

got 405 method not allowed when create issue with REST API in C#, it worked in cURL

Andy Kuan July 27, 2015

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;
        }

 

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
Andy Kuan July 27, 2015

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.

 

Volodymyr Krupach
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 27, 2015

> I don't know how to express my appreciation for your answer Just accept/upvote it :-)

2 votes
Volodymyr Krupach
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 27, 2015

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.

Andy Kuan July 29, 2015

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?

Volodymyr Krupach
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 29, 2015

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

Andy Kuan July 29, 2015

sure. I will do that. thanks.

Jim Coryat February 20, 2017

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.

Like Said Kouzibry likes this
TAGS
AUG Leaders

Atlassian Community Events