Error when using TFS API concerning updating work items( HTTP Status 500 )

拟墨画扇 提交于 2019-12-12 05:37:03

问题


I am trying to update a field(/fields/System.Description) of a work-item on TFS using the TFS API according to the official Microsoft document. There is something wrong with the api "Update work items" when using the sample code listed on the page, the returning status code of the response is 500 if I want to "add" or "replace" a certain filed, but success if I want to "test" a value, can anyone please give me some help?

using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

using Newtonsoft.Json;

public static void UpdateWorkItemUpdateField()
    {
        string _personalAccessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        string _credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _personalAccessToken)));
        string _id = "111";

        Object[] patchDocument = new Object[1];

        patchDocument[0] = new { op = "replace", path = "/fields/System.Description", value = "Changing the description done" };


        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-patch+json"); // mediaType needs to be application/json-patch+json for a patch call
            var method = new HttpMethod("PATCH");
            var request = new HttpRequestMessage(method, "http://mysite:8080/tfs/MyCollection/_apis/wit/workitems/" + _id + "?api-version=1.0") { Content = patchValue };
            var response = client.SendAsync(request).Result;

            if (response.IsSuccessStatusCode)
            {
                var result = response.Content.ReadAsStringAsync().Result;
            }
        }
    }

and here is the return status:

{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Pragma: no-cache X-TFS-ProcessId: xxxxxxxxxxxxxxxx X-FRAME-OPTIONS: SAMEORIGIN X-VSS-UserData: xxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxx ActivityId: xxxxxxxxxxxxxxxxxxxxxxxxxxxx X-TFS-Session: xxxxxxxxxxxxxxxxx Lfs-Authenticate: NTLM X-Content-Type-Options: nosniff Cache-Control: no-cache Date: Thu, 01 Jun 2017 06:50:04 GMT P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT" Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 428 Content-Type: application/json; charset=utf-8 Expires: -1}}


回答1:


I tried to make the same request but only without the MyCollection part in the url and it worked.

try to do it yourself. Danny



来源:https://stackoverflow.com/questions/44300710/error-when-using-tfs-api-concerning-updating-work-items-http-status-500

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!