2017-05-31 62 views
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; 
    } 
    } 
} 

回答

0

您使用的是错误的mediaType的,应该是 '应用/ JSON-补丁+ JSON' 而不是 '应用程序/ JSON'。

var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json"); 

在代码示例中,它已经注意到你:

// mediaType的需要为应用程序/ JSON-补丁+ JSON的补丁呼叫

你调用修补程序调用,因此请将mediaType更改为application/json-patch+json

+0

感谢您的帮助,错误代码400在更改mediaType后消失。之后,我试图更改某个工作项目的/fields/System.Description,但http状态的返回值为400,您是否有任何有关错误代码的信息?谢谢! – RickyXRQ

+0

@RickyXRQ当你发布新的问题,在这里发布新的链接,我会帮你。 –

+0

https://stackoverflow.com/questions/44300710/error-when-using-tfs-api-concerning-updating-work-items-http-status-500 @ Tingting0929-MSFT – RickyXRQ

相关问题