我知道這個問題已經被問過,但我似乎無法得到它太工作。C#使用REST API在Jira中關閉一個問題
我能夠通過Jira進行身份驗證並使用JSON字符串創建新票證,但試圖關閉相同問題會產生「400錯誤請求」錯誤。
代碼:
public string jiraJSON;
public void openJira()
{
jiraJSON = string.Format(@"{{""fields"":{{""assignee"":{{""name"":""{0}""}},""project"":{{""key"":""TS""}},""summary"":""{1}"",""description"":""{2}"",""issuetype"":{{""name"":""Unplanned Event""}} }} }}", jiraUsernameTextBox.Text, jiraSummary, jiraDescription);
Dictionary<string, string> jiraResponse = sendHTTPtoJIRA(jiraJSON,"open","");
}
public void closeJira(string jiraKey)
{
jiraJSON = @"{{""update"":{{""comment"":[{{""add"":{{""body"":""Done""}}}}]}},""fields"":{{""resolution"":{{""name"":""Done""}}}},""transition"":{{""id"":""51""}}}}";
jiraResponse = sendHTTPtoJIRA(jiraJSON,"close",jiraKey);
}
private Dictionary<string,string> sendHTTPtoJIRA(string json, string operation,string issueID)
{
string restURL="";
string method = "";
switch (operation)
{
case "open":
restURL = string.Format("{0}rest/api/2/issue/", jiraURL);
method = "POST";
break;
case "close":
restURL = string.Format("{0}rest/api/2/issue/{1}/transitions/?expand=transitions.fields", jiraURL,issueID);
method = "POST";
break;
}
HttpWebResponse response = null;
HttpWebRequest request = WebRequest.Create(restURL) as HttpWebRequest;
request.Method = method;
request.Accept = "application/json";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Basic " + authenticateJira());
byte[] data = Encoding.UTF8.GetBytes(json);
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
requestStream.Close();
}
using (response = request.GetResponse() as HttpWebResponse)
{
var reader = new StreamReader(response.GetResponseStream());
string str = reader.ReadToEnd();
displayMessages(string.Format("The server returned '{0}'\n{1}", response.StatusCode, str), "white", "purple");
var jss = new System.Web.Script.Serialization.JavaScriptSerializer();
var sData = jss.Deserialize<Dictionary<string, string>>(str);
sData.Add("code", response.StatusCode.ToString());
request.Abort();
return sData;
}
}
我轉述的代碼清晰,但「sendHTTPtoJIRA」方法的緣故一點點逐字和jiraJSON串也是一字不差。
使用此代碼,我可以打開一個問題並將其分配給我自己,但是當我嘗試關閉該問題時,我收到一個「400錯誤請求」,它告訴我我的jiraJSON字符串在「closeJira 「方法不正確。
異常着陸在「使用(response = request.GetResponse()as HttpWebResponse)」和引用「jiraResponse = sendHTTPtoJIRA(jiraJSON,」close「,jiraKey);」作爲調用該方法的路線,所以我知道它在嘗試解決問題時是錯誤的。
從其他職位的常見問題,我已經解決:
- 我使用有權限關閉問題的用戶帳戶。
- 我試過了「POST」和「PUT」方法。使用「PUT」會產生「405方法不允許」的錯誤。
- 躲過大括號和引號。 我使用關閉問題
壓縮擴大JSON字符串:
{{
""update"":{{""comment"":[{{""add"":{{""body"":""Done""}}}}]}},
""fields"":{{""resolution"":{{""name"":""Done""}}}},
""transition"":{{""id"":""51""}}
}}
我試過的變化,當然。似乎沒有任何工作。我也包括了51和無引號無濟於事。
當我瀏覽到http://jira/rest/api/2/issue/TS-1000/transitions/?expand=transitions.fields 我得到下面的輸出(這是我得到了「51」在我jiraJSON字符串ID):
{
"expand": "transitions",
"transitions": [{
"id": "11",
"name": "Start Progress",
"to": {
"self": "http://jira/rest/api/2/status/3",
"description": "This issue is being actively worked on at the moment by the assignee.",
"iconUrl": "http://jira/images/icons/statuses/inprogress.png",
"name": "In Progress",
"id": "3",
"statusCategory": {
"self": "http://jira/rest/api/2/statuscategory/4",
"id": 4,
"key": "indeterminate",
"colorName": "yellow",
"name": "In Progress"
}
},
"fields": {
"attachment": {
"required": false,
"schema": {
"type": "array",
"items": "attachment",
"system": "attachment"
},
"name": "Attachment",
"operations": []
},
"assignee": {
"required": true,
"schema": {
"type": "user",
"system": "assignee"
},
"name": "Assignee",
"autoCompleteUrl": "http://jira/rest/api/latest/user/assignable/search?issueKey=TS-2034&username=",
"operations": ["set"]
}
}
}, {
"id": "51",
"name": "Close Issue",
"to": {
"self": "http://jira/rest/api/2/status/6",
"description": "The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.",
"iconUrl": "http://jira/images/icons/statuses/closed.png",
"name": "Closed",
"id": "6",
"statusCategory": {
"self": "http://jira/rest/api/2/statuscategory/3",
"id": 3,
"key": "done",
"colorName": "green",
"name": "Done"
}
},
"fields": {
"resolution": {
"required": true,
"schema": {
"type": "resolution",
"system": "resolution"
},
"name": "Resolution",
"operations": ["set"],
"allowedValues": [{
"self": "http://jira/rest/api/2/resolution/1",
"name": "Fixed",
"id": "1"
}, {
"self": "http://jira/rest/api/2/resolution/5",
"name": "Cannot Reproduce",
"id": "5"
}, {
"self": "http://jira/rest/api/2/resolution/3",
"name": "Duplicate",
"id": "3"
}, {
"self": "http://jira/rest/api/2/resolution/4",
"name": "Incomplete",
"id": "4"
}, {
"self": "http://jira/rest/api/2/resolution/7",
"name": "Review Completed",
"id": "7"
}, {
"self": "http://jira/rest/api/2/resolution/6",
"name": "Unresolved",
"id": "6"
}, {
"self": "http://jira/rest/api/2/resolution/2",
"name": "Won't Fix",
"id": "2"
}, {
"self": "http://jira/rest/api/2/resolution/10000",
"name": "Done",
"id": "10000"
}, {
"self": "http://jira/rest/api/2/resolution/10100",
"name": "Edgewater Review",
"id": "10100"
}, {
"self": "http://jira/rest/api/2/resolution/10200",
"name": "Active Project",
"id": "10200"
}, {
"self": "http://jira/rest/api/2/resolution/10300",
"name": "Won't Do",
"id": "10300"
}
]
},
"customfield_10652": {
"required": false,
"schema": {
"type": "string",
"custom": "com.atlassian.jira.plugin.system.customfieldtypes:textarea",
"customId": 10652
},
"name": "Resolution Activity",
"operations": ["set"]
}
}
}
]
}
那我和我的JSON字符串在做什麼?任何建議,將不勝感激。謝謝!
您是否驗證過您生產合法的json?你應該考慮使用一個庫,比如Json.Net,然後將對象層次序列化爲json。如果您的數據包含撇號或類似的無效字符,除非正確處理,否則使用當前代碼可能會導致問題。 –