0
我正嘗試使用TFS API根據Microsoft document更新TFS上的工作項的字段。使用頁面上列出的示例代碼時,api「更新工作項目」有問題,響應的返回狀態代碼爲400.不管我做了什麼,狀態代碼始終爲400,任何人都可以給我一些幫助?使用TFS API時有關更新工作項目(HTTP狀態400)的錯誤
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
public void UpdateWorkItemUpdateField()
{
string _personalAccessToken = "xxxxxxxxxxxxxx";
string _credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _personalAccessToken)));
string _id = "111";
Object[] patchDocument = new Object[3];
patchDocument[0] = new { op = "test", path = "/rev", value = "1" };
patchDocument[1] = new { op = "add", path = "/fields/Microsoft.VSTS.Common.Priority", value = "2" };
patchDocument[2] = new { op = "add", path = "/fields/System.History", value = "Changing priority" };
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);
var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json"); // mediaType needs to be application/json-patch+json for a patch call
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, "https://accountname.visualstudio.com/_apis/wit/workitems/" + _id + "?api-version=2.2") { Content = patchValue };
var response = client.SendAsync(request).Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
}
}
}
感謝您的幫助,錯誤代碼400在更改mediaType後消失。之後,我試圖更改某個工作項目的/fields/System.Description,但http狀態的返回值爲400,您是否有任何有關錯誤代碼的信息?謝謝! – RickyXRQ
@RickyXRQ當你發佈新的問題,在這裏發佈新的鏈接,我會幫你。 –
https://stackoverflow.com/questions/44300710/error-when-using-tfs-api-concerning-updating-work-items-http-status-500 @ Tingting0929-MSFT – RickyXRQ