I have been trying to set up a function to call the Jira Rest API with parameters to then be able to create an issue in Jira. To call the Rest API Jira provided a documentation which describes how to call the Rest API here. On the website, the CURL and the JSON are given. Here is the REST API request I tried to set up in C#
:
curl -D- -u peno.ch:yourpassword -H "Content-Type: application/json" --data @foo.json https://jira-test.ch.*******.net/rest/api/latest/issue/
This is the foo.json payload:
{
"fields": {
"project":
{
"key": "FOO"
},
"summary": "Test the REST API",
"issuetype": {
"name": "Task"
}
}
}
I have tried to Implement a HttpWebRequest
to call the Rest API and also I have tried with a WebClient
. None of them worked. I kind of understand the API but I think I haven't got the parameters right, I think I am doing something wrong there. Also on Google, I didn't find any solution.
I am getting an Internal error from Jira when executing the function below. (no specific information on the error)
public static void CreateJiraRequest(JiraApiObject jiraApiObject)
{
string url = "https://jira-test.ch.*******.net/rest/api/latest/issue/";
string user = "peno.ch";
string password = "****";
var client = new WebClient();
string data = JsonConvert.SerializeObject(jiraApiObject);
client.Credentials = new System.Net.NetworkCredential(user, password);
client.UploadStringTaskAsync(url, data);
}
This is my JiraApiObject which exactly translates to the Json payload shown above.
public class JiraApiObject
{
public class Project
{
public string key { get; set; }
}
public class Issuetype
{
public string name { get; set; }
}
public class Fields
{
public Project project { get; set; }
public Issuetype issuetype { get; set; }
public string summary { get; set; }
}
public class RootObject
{
public Fields fields { get; set; }
}
}
When I execute the CURL command on the console everything works I just couldn't figure out how to structure the WebClient
or HttpWebRequest
.
I find that many Jira users face this problem and there is not a single good solution I could find on the internet. I hope to find a solution and help others who have the same problem by raising this question.