为什么在使用RESTAPI(C#)创建JIRA问题时出现错误?
我正在处理一个需要使用RESTAPI一次性创建多个问题的需求。然而,我从上传一个问题开始,因为我是API集成方面的新手。我用c#写了几行代码。这是我的密码:为什么在使用RESTAPI(C#)创建JIRA问题时出现错误?,c#,json,jira,jira-rest-api,C#,Json,Jira,Jira Rest Api,我正在处理一个需要使用RESTAPI一次性创建多个问题的需求。然而,我从上传一个问题开始,因为我是API集成方面的新手。我用c#写了几行代码。这是我的密码: static void Main(string[] args) { JavaScriptSerializer jss = new JavaScriptSerializer(); JiraCreateIssueRequest jcir = new JiraCreateIssueRequest();
static void Main(string[] args)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
JiraCreateIssueRequest jcir = new JiraCreateIssueRequest();
//////////////////////////////////////////////////////////////////
string sUsername = "aaa@xyz.com";
string sPassword = "TestPassword";
string uri = @"https://domain.atlassian.net/rest/api/2/issue";
Uri address = new Uri(uri);
HttpWebRequest request;
//HttpWebResponse response = null;
StreamReader sr;
string sData = null;
string returnXML = string.Empty;
if (address == null) { throw new ArgumentNullException("address"); }
//jcir.project.ID = 100;
//jcir.Summary = "This issue is created by JIRA REST Api";
//jcir.Description = "This issue is created by JIRA REST Api";
//jcir.IssueType.ID = 1;
sData = @"{""fields"":{""project"":{""key"": ""SITT""},""summary"": ""REST API Uploading"",""description"":""Creating an issue via REST API"",""issuetype"": {""name"": ""Test""}}}";
//sData = jcir.ToString();
try
{
// Create and initialize the web request
request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json";
// Add the Authorization header to the web request
request.Credentials = new NetworkCredential(sUsername, sPassword);
//Write Data
if (sData != null)
{
byte[] byteData = UTF8Encoding.UTF8.GetBytes(sData);
// Set the content length in the request headers
request.ContentLength = byteData.Length;
// Write data
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
string str = reader.ReadToEnd();
}
}
}
catch (WebException wex)
{
// This exception will be raised if the server didn't return 200 - OK
// Try to retrieve more information about the error
if (wex.Response != null)
{
using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
{
try
{
string sError = string.Format("The server returned '{0}' with the status code {1} ({2:d}).",
errorResponse.StatusDescription, errorResponse.StatusCode,
errorResponse.StatusCode);
sr = new StreamReader(errorResponse.GetResponseStream(), Encoding.UTF8);
returnXML = sr.ReadToEnd();
}
finally
{
if (errorResponse != null) errorResponse.Close();
}
}
}
else
{
throw new Exception(wex.Message);
}
}
}
public class JiraCreateIssueRequest
{
protected JavaScriptSerializer jss = new JavaScriptSerializer();
public JiraProject project = new JiraProject();
public string Summary { get; set; }
public string Description { get; set; }
public JiraIssueType IssueType = new JiraIssueType();
public override string ToString()
{
return jss.Serialize(this);
}
}
public class JiraCreateIssueResponse
{
}
public class JiraProject
{
public int ID { get; set; }
//public string Key { get; set; }
}
public class JiraIssueType
{
public int ID { get; set; }
//public string Name { get; set; }
}
但是当我运行上面的代码时,我得到了“400”错误。我在谷歌上搜索发现,当URL或用户名/密码不正确时,通常会出现这种错误。我反复核对了这两件事,但都是正确的
我可以知道为什么会出现这个错误,或者问题的解决方法是什么吗 您的密码不是您的登录密码,而是您从这里获得的API令牌:
生成一个令牌,然后将其用作您的密码。Http 400的出现是因为服务器无法或将不会处理该请求,因为出现了明显的客户端错误(例如,格式错误的请求语法、太大的大小、无效的请求消息框架或欺骗性的请求路由。您能否检查请求的所有属性是否都存在。感谢Bhaskar,问题已经解决。我对属性和Json数据进行了一些更改。此外,您能否指导我如何在Jira中创建Epic问题类型。这代码在正常发行类型下运行良好,但在史诗发行类型下不起作用。