使用JIRA Rest API创建问题会返回HTTP状态代码200,但未创建问题

时间:2019-08-09 11:36:02

标签: c# rest jira jira-rest-api

我想使用C#使用Jira Rest API创建一个问题

            string data = @"{ ""fields"": { 
                                ""project"":
                   {
                       ""key"": ""TOTEM""
                   },
                                ""summary"": ""just a test"",
                                ""description"": ""Creating of an issue using project keys and issue type names using the REST API"",
                                ""issuetype"": {
                                    ""name"": ""Task""
                                },
                                ""assignee"": { ""name"": ""imane.elbarchi"" }
                            }
            }";

            //Console.WriteLine(data);

            string uri = "https://proactioneu.ent.cgi.com/rest/api/latest/issue";

            System.Net.Http.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("username:password");

            //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");

            System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;

            Console.WriteLine(response);
            Console.ReadKey();
        }
    }
}

我得到一个类似的回复:

  

状态码:200,原因短语:“已找到”,版本:1.0,内容:   System.Net.Http.HttpConnection + HttpConnectionResponseContent,标头:       {         缓存控制:无缓存         连接方式:关闭         内容类型:text / html       }

但未创建问题。

1 个答案:

答案 0 :(得分:1)

我认为是因为这一行:

   System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;

创建问题时,我使用:

                var response = await httpClient.PostAsync(httpClient.BaseAddress, content);

因此第一个参数需要您要将内容发送到的网址。 “问题”不是网址。

我的代码如下所示。也许您可以使用它。

        public async Task<bool> PostIssueAsync(string userpass, string data)
    {
        HttpClient httpClient = new HttpClient();

        httpClient.BaseAddress = new Uri(Constants.JiraUrl + "rest/api/latest/issue");

        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", userpass);

        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var content = new StringContent(data, Encoding.UTF8, "application/json");
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        try
        {
            var response = await httpClient.PostAsync(httpClient.BaseAddress, content);
            return response.IsSuccessStatusCode;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            return false;
        }
    }