Componenet creatation failed with 404 in C# and REST

Ben Liu July 21, 2013

Hi there,

I am using REST API and C# to create a component under a project, but it fails with 404 error.

I used RESTClient, an add-on for firefox to verify the problem and found the response is 404 and "No project could be found with key xxxx". The project key indeed exists in JIRA.

Could someone kindly tell me any hints?

Thank you.

3 answers

0 votes
Ben Liu July 29, 2013
public enum JiraResource
    {
        project,
        issue,
        component,
        workflow,
        workflowscheme,
        group,
        groups
    }
public class JiraRestSvc
    {
        private const string m_BaseUrl = "http://localhost:8080/rest/api/2/";

        // Greenhopper REST API
        // https://jira.atlassian.com/plugins/servlet/restbrowser#/com-pyxis-greenhopper-jira-greenhopper-rest-filter
        // http://localhost:8080/rest/greenhopper/1.0/project-list
        private string m_Username;
        private string m_Password;

        public JiraRestSvc(string username, string password)
        {
            m_Username = username;
            m_Password = password;
        }

        private string GetEncodedCredentials()
        {
            string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);
            byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
            return Convert.ToBase64String(byteCredentials);
        }

        protected string RunQuery(JiraResource resource,
                                string argument = null,
                                string data = null,
                                string method = "GET")
        {
            string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString());

            if (argument != null)
            {
                url = string.Format("{0}{1}/", url, argument);
            }

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.ContentType = "application/json";
            request.Method = method;

            if (data != null)
            {
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(data);
                }
            }

            string base64Credentials = GetEncodedCredentials();
            request.Headers.Add("Authorization", "Basic " + base64Credentials);

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            string result = string.Empty;
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }

            return result;
        }
public void CreateComponent()
        {
            string post_json = "{\"name\": \"Test Component\", \"description\": \"This is a test component from program\", \"leadUserName\": \"xxx\", \"asigneeType\": \"PROJECT_LEAD\", \"isAssigneeTypeValid\": false, \"project\": \"TEST\"}";

            try
            {
                string resp_json = RunQuery(JiraResource.component, null, post_json, "POST");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
}

I would like to create component under existing project with key "TEST", and it failed.

0 votes
Ben Liu July 28, 2013

Hi Pedro,

I am using a unique account which administers JIRA, so this problem should not be caused by permission limitation. How do you think? Any other clues?

Thank you.

RambanamP
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 28, 2013

can you share your code?

0 votes
Pedro Cora
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 22, 2013

Ben,

Check if the user logging in to retrieve the REST call has permissions to browse the issues through the interface. This user must be in the Browse Project permission in the project's Permission Scheme.

Suggest an answer

Log in or Sign up to answer