使用JIRA rest API创建问题返回错误方法不允许

时间:2016-02-07 11:34:59

标签: api post jira

我试图通过以下代码使用JIRA rest API创建问题

 string data = @"{ ""fields"": {
                            ""project"":
               {
                   ""key"": ""TEST""
               },
                            ""summary"": ""Test Ticket"",
                            ""description"": ""Creating of an issue using project keys and issue type names using the REST API"",
                            ""issuetype"": {
                                ""name"": ""Bug""
                            },
                            ""assignee"": { ""xx"": ""xxx"" }
                        }
        }";
        string postUrl = "http://localhost:8080/rest/api/latest/issue/";
        System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
        client.BaseAddress = new System.Uri(postUrl);
        byte[] cred = UTF8Encoding.UTF8.GetBytes("xx:xx");
        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();

        var content = new StringContent(data, Encoding.UTF8, "application/json");
        //System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent<Issue>(data, jsonFormatter);
        System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;

我收到了意外错误:&#34;方法不允许&#34;

1 个答案:

答案 0 :(得分:0)

我是第二个@Robert,permission的第一个参数叫PostAsync,所以你的requestUri来了。然后,您可以跳过postUrl设置。

由于您有BaseAddress且相对URI为BaseAddress,因此HttpClient很可能将它们合并到requestUri并在实际POST请求中使用此URL,并且该目标不允许POST。