【发布时间】:2014-10-29 06:53:41
【问题描述】:
我正在使用我的 json 数据向 JIRA 发送 POST 请求以创建项目,但我无法在 JIRA 中创建项目,我试图从 Fiddler 中查看错误,但出现以下错误。我正在使用 C# 并为其创建了控制台应用程序。
我发布的 JSON 数据如下。
{
"fields": {
"project": {
"key": "JTL"
},
"issuetype": {
"name": "BUG"
}
}
}
错误信息如下:
{"errorMessages":[],"errors":{"issuetype":"issue type is required"}}
我正在从以下代码发布 json 数据,请指出我错了什么以及哪里错了?
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;
在上面的代码中,您可以看到我正在发送授权用户的凭据并发送数据。
【问题讨论】:
-
您是否 100% 肯定您的 JSON 输出数据包含
issuebug键? -
是的,出于我的积极性,有理由在我当前的 json 描述和摘要键中不存在,但是当我将 json 数据连同两个键一起发送时,响应是“summery”和“description”是未知的或不是必需的,比我删除了这两个键值,但在 JIRA REST api 文档中,这两个键都在示例中用于演示。
-
您确定您访问的是正确的端点吗?表示正确的 URL?
-
是的,我确定我的网址,这里也有冲突,在 JIRA api doc 中,网址是 example.com:8080/jira/rest/api/2/issue 我正在使用以下方式 myCompany.atlassian.net/rest/api/2/issue 这对我有用。
标签: c# json jira dotnet-httpclient jira-rest-api