Create an Issue Using JIRA API

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace MyWorkView
{
    class Program
    {
        static void Main(string[] args)
        {
            var data = new Issue();
            data.fields.project.key = "WKV";
            data.fields.summary = "Created By JIRA API";
            data.fields.description = "Created By JIRA API";
            data.fields.issuetype.name = "Incident";
            //data.fields.Assignee = "";
            //data.fields.Reporter = "Ravi Govindasamy";
            string postUrl = @"https://url.com:8443/rest/api/2/";
            byte[] cred = UTF8Encoding.UTF8.GetBytes("lanid:landpassword");
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

            client.BaseAddress = new System.Uri(postUrl);
            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(data, jsonFormatter);
            System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;

            if (response.IsSuccessStatusCode)
            {
                string result = response.Content.ReadAsStringAsync().Result; // its will be 200 OK (inserted)
                Console.Write(result);
            }
            else
            {
                Console.Write(response.StatusCode.ToString());
                Console.ReadLine();
            }
        }
    }
    public class Issue
    //Create C# class to meet json object , to post this json into rest API
    {
        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 string Reporter { get; set; }
        //public string 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; }
    }
}

Comments

Popular posts from this blog

IBM FileNet: Bulk Processing using JavaScript

DB2 Date Time Functions

File Net Insert Document