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

标签 azure restsharp

我正在使用 RestSharp v107。

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

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 - 进行了相应的更改。仍然没有帮助。

最佳答案

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

您需要做的就是:

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

关于azure - RestSharp PATCH API 调用更新 Azure Devops 工作项抛出 BAD 请求 - "You must pass a valid patch document in the body of the request.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71747362/

相关文章:

c# - PopReceipt 属性是否保证 CloudQueueMessage 已成功添加到 Azure 存储队列?

c# - Restsharp:反序列化具有比某些类更少/更多字段的 json 对象

c# - RestSharp 接受 header 更改

c# - RestSharp 可以在不使用多部分内容类型的情况下发送二进制数据吗?

c# - 使用动态对象反序列化 JSON

c# - Azure WCF 服务上的 maxReceivedMessageSize 太小

azure 。如何在高可用 Web 服务器之间共享文件

azure - 升级部署后,服务陷入过渡状态

Azure Web服务每隔几个小时更新一次相同的docker镜像

c# - RestSharp 反序列化问题