RestSharp PATCH API 调用更新 Azure Devops 工作项抛出 BAD 请求 - "You must pass a valid patch document in the body of the request."

RestSharp PATCH API call to update Azure Devops work item throwing BAD Request - "You must pass a valid patch document in the body of the request."

我正在使用 RestSharp v107。

我想更新测试用例的迭代路径。我可以使用 Postman 更新它,但使用 RestSharp 我收到“错误请求”-“您必须在请求正文中传递有效的补丁文档。”

Azure Devops Update Work Item Rest API

我的代码:

        var client = new RestClient(@"https://dev.azure.com/krakhil/AkhilProject/_apis/wit/workitems/25?api-version=6.1-preview.3&Authorization=Basic BASE64PATSTRING");
        client.Authenticator = new HttpBasicAuthenticator("", "My_PAT_I_HAVE_GENERATED");

        var request = new RestRequest();
        request.AddHeader("Content-Type", "application / json - patch + json");

        var body = @"[
" + "\n" +
@"      {
" + "\n" +
@"        ""op"": ""add"",
" + "\n" +
@"        ""path"": ""/fields/System.IterationPath"",
" + "\n" +
@"        ""value"": ""AkhilProject\Sprint 1""
" + "\n" +
@"      }
" + "\n" +
@"]";

        request.AddParameter("application/json-patch+json", body, ParameterType.RequestBody);

        var response = client.PatchAsync(request).Result;

我已尝试使用“AddJSONBody”、ExecuteAsync - 并进行了相应的更改。仍然没有帮助。

RestSharp 工作代码 - 根据 A​​lexey Zimarev

的输入
var client = new RestClient(@"https://dev.azure.com/krakhil/AkhilProject/_apis/wit/workitems/25?api-version=6.1-preview.3");
        client.Authenticator = new HttpBasicAuthenticator("", "YOUR PAT");

        var request = new RestRequest();
        request.AddHeader("Content-Type", "application/json-patch+json");

        var body = @"[
" + "\n" +
@"      {
" + "\n" +
@"        ""op"": ""add"",
" + "\n" +
@"        ""path"": ""/fields/System.IterationPath"",
" + "\n" +
@"        ""value"": ""AkhilProject\Sprint 1""
" + "\n" +
@"      }
" + "\n" +
@"]";

        request.AddStringBody(body, DataFormat.Json);

        var response = client.PatchAsync(request).Result;

使用 HTTP 客户端的工作代码

        string personalaccesstoken = "MY_PAT";
        string organisation = "krakhil";
        string project = "AkhilProject";

        using HttpClient client = new HttpClient();

        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
            Convert.ToBase64String(
                System.Text.ASCIIEncoding.ASCII.GetBytes(
                    string.Format("{0}:{1}", "", personalaccesstoken))));

        var obj = new[] { new { from = (string?)null, op = "add", path = "/fields/System.IterationPath", value = "AkhilProject\Sprint 1" } };
        
        string serialisedObj = System.Text.Json.JsonSerializer.Serialize(obj);

        var content = new StringContent(
            serialisedObj,
            Encoding.UTF8,
            "application/json-patch+json");

        HttpResponseMessage response = client.PatchAsync($"https://dev.azure.com/{organisation}/{project}/_apis/wit/workitems/25?api-version=6.1-preview.3",content).Result;

        response.EnsureSuccessStatusCode();
        string responseBody =  response.Content.ReadAsStringAsync().Result;

有时只阅读文档会有所帮助。

您需要做的就是:

var request = new RestRequest()
    .AddStringBody(body, "application/json-patch+json");