Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating jira issue via Rest c# httpClient

I have read one answer on atlassian https://answers.atlassian.com/questions/79902/using-httpclient-c-to-create-a-jira-issue-via-rest-generates-bad-request-response where one user created a JIRA issue by the following code. I adapted it but get an error by using a self-build class issue with ObjectContent

Http.HttpContent content = new Http.ObjectContent<Issue>(data, jsonFormatter);

The compiler wont accept it. Does anybody know why?

 public string CreateJiraIssue()
        {

            string data= @"{ ""fields"": { 
                                ""project"":
                   {
                       ""key"": ""HELP""
                   },
                                ""summary"": ""Test Ticket"",
                                ""description"": ""Creating of an issue using project keys and issue type names using the REST API"",
                                ""issuetype"": {
                                    ""name"": ""Ticket""
                                },
                                ""assignee"": { ""name"": ""user"" }
                            }
            }";
            string postUrl = "https://xxx.jira.com/rest/api/2/";
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            client.BaseAddress = new System.Uri(postUrl);
            byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
            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"));
            System.Net.Http.Formatting.MediaTypeFormatter jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();

            System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent<Issue>(data, jsonFormatter);
            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();
            }

And using

namespace IOnotification_System
{
    public class Issue
    {
        public Fields fields { get; set; }
        public Issue()
        {
            fields = new Fields();
        }
    }

    public class Fields
    {
        public Project project { get; set; }
        public string summary { get; set; }
        public string description { get; set; }
        public Assignee assignee { get; set; }
        public IssueType issuetype { get; set; }
        public Fields()
        {
            project = new Project();
            issuetype = new IssueType();
        }
    }

    public class Project
    {
        public string key { get; set; }
    }

    public class IssueType
    {
        public string name { get; set; }
    }
     public class Assignee
    {
        public string name { get; set; }
    }
}
like image 825
Simon Avatar asked Oct 22 '22 21:10

Simon


1 Answers

EDIT

The message clearly says that System.Net.Http.ObjectContent() expects an Issue object for its first parameter. I expect there is another message right after that saying that there is no conversion possible from a string to an Issue.

You are passing a string to a method that expects an Issue object. The formatter is used to convert an Issue object to a Json string.

You already have the string, so there is no point in trying to convert it. You only need the formatter if you have an Issue instance which you want to convert to a Json string. You can use the StringContent class and use its Headers property to add any headers not already set on the client, eg:

var content=new StringContent(data);

Original

What is the error message and what kind of project are you using? The System.Net.Http.Formatting namespace is part of ASP.NET Web API. Are you building an ASP.NET application, a console application, something else?

Unless you ARE building an ASP.NET site this code won't work. If your only issue is how to parse Json requests, just use another Json deserialization class. Json.NET is a very popular choice.

In any case there is no reason to use a Json class to convert a string to an HttpContent object containing that exact same string. You can use the StringContent class and use its Headers property to add any headers not already set on the client.

like image 144
Panagiotis Kanavos Avatar answered Oct 27 '22 08:10

Panagiotis Kanavos