简体   繁体   English

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

[英]Creating an issue using JIRA Rest API returns HTTP status code 200 but the issue is not created

I want to create an issue with Jira Rest API using C#我想使用 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();
        }
    }
}

and I get a response like:我得到如下回复:

StatusCode: 200, ReasonPhrase: 'Found', Version: 1.0, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers: { Cache-Control: no-cache Connection: close Content-Type: text/html } StatusCode: 200, ReasonPhrase: 'Found', Version: 1.0, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers: { Cache-Control: no-cache Connection: close Content-Type: text/html }

but the issue is not created.但问题并没有产生。

I think it is because of this line:我认为这是因为这一行:

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

When I create an issue I use:当我创建一个问题时,我使用:

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

So the first parameter wants the url you want to send the content to.因此,第一个参数需要您要将内容发送到的 url。 "issue" is not an url. “问题”不是网址。

My code looks like this.我的代码看起来像这样。 Maybe you can use it.也许你可以使用它。

        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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM