Create (post) JIRA ticket (Issue) with rest API using C# ASP.NET
Today I will show you how to create new JIRA ticket using C#, you can use this code in class library then you can use it in any .NET application (like ASP.NET).using JIRA web service(REST API) you can retrieve and post Jira tickets by creating rest call to web services,for example :
http://localhost:8080/rest/api/latest/issue/PROJ-2
calling this url will return json object contains the full information about issue with key (PROJ-2) so each issue or project have a key.
http://localhost:8080/rest/api/latest/issue/PROJ-2
calling this url will return json object contains the full information about issue with key (PROJ-2) so each issue or project have a key.
Quick helpful links
see how to install Jira on your windows
JIRA rest API POST
Generate C# class from JSON
see how to install Jira on your windows
JIRA rest API POST
Generate C# class from JSON
Now to post new issue follow the instructions :
Create Console Application Project in Visual Studio.
Download the following libraries by NuGet :
- Json.NET
- Microsoft ASP.NET Web API 2 Client
- Microsoft HTTP Client Libraries
Here is the C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace ConsoleApplication5
{
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 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 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 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; }
}
/* Now we will create object with (Issue) and then we will fill it with needed info , this we wil lserialize it and pass it to rest API */
class Program
{
static void Main(string[] args)
{
var data = new Issue();
data.fields.project.key = "PRJT";
data.fields.summary = "test";
data.fields.description = "test";
data.fields.issuetype.name = "Improvement";
string postUrl = "http://localhost:8080/rest/api/latest/";
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
client.BaseAddress = new System.Uri(postUrl);
byte[] cred = UTF8Encoding.UTF8.GetBytes("user: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<string>(data, jsonFormatter);
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; // its will be 200 OK (inserted)
Console.Write(result);
}
else
{
Console.Write(response.StatusCode.ToString());
Console.ReadLine();
}}}}
Hope that you find this tutorial help.
Hi I'm getting MethodNotAllowed in console. Could you please help?
ReplyDeleteNice example to start with. It worked straight away.
ReplyDeletehi i'm not updating the comment using above code please help me
ReplyDeletehi i'm not updating the comment and issue status from above given code please help me
ReplyDelete