关于c#:如何通过REST API向JIRA创建问题?

How to Create a Issue into JIRA through REST api?

我正在使用json数据将POST请求发送到JIRA,以创建一个项目,但是我无法在JIRA中创建一个项目,我试图从Fiddler看到错误,但出现以下错误。 我正在使用C#并为此创建了控制台应用程序。

我正在发布的JSON数据如下。

1
2
3
4
5
6
7
8
9
10
{
"fields": {
    "project": {
        "key":"JTL"
     },
    "issuetype": {
        "name":"BUG"
     }
  }
}

错误消息如下:

{"errorMessages":[],"errors":{"issuetype":"issue type is required"}}

我正在从以下代码发布json数据,请指出我在哪里以及哪里出错了?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
string data=@"{"fields":{"project":{"key":"JTL"},"issuetype":{"name":"BUG"}}}";

//object of HttpClient.
HttpClient client = new HttpClient();

//Putting URI in client base address.
client.BaseAddress = new Uri(uri);

//Putting the credentials as bytes.
byte[] cred = UTF8Encoding.UTF8.GetBytes("jiraUserName" +":" +"JiraPassword");

//Putting credentials in Authorization headers.
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));

//Putting content-type into the Header.
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

//I am using StringContent because I am creating console application, no any serialize I used for manipulate the string.
var content = new StringContent(data, Encoding.UTF8,"application/json");

//Sending the Post Request to the server. But getting 400 bad Request.
System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;

在上面的代码中,您可以看到我正在发送用于授权用户的凭据并发送数据。


如下更改数据:

1
string data = @"{'fields':{'project':{'key':'JTL'},'summary':'Sample issue','description':'Creating an issue via REST API','issuetype':{'name':'Bug'}}}";


我已经解决了我的问题。 我对代码进行了少量更改,并且代码运行成功。
我更改了网址。

1
2
Old Url: https://MyCompany.atlassian.net/rest/api/2/issue
new url: https://MyCompany.atlassian.net/rest/api/latest/issue

在Json中,我做了小的更改,在" issuetype"名称中是Bug,该错误当前在我的帐户中不可用,当前在我的帐户中" TASK"问题类型可用,因此我将issutype名称从" Bug"更改为" Task"。 现在它成功地工作了!感谢上帝,这浪费了我很多时间。 悲伤:(

还要感谢Abdurrahman Koken。 :)干杯!