Azure DevOps:尝试使用API​​更新工作项状态时遇到错误

发布于 2025-02-13 17:00:33 字数 898 浏览 2 评论 0原文

我正在尝试编写一个代码来通过Azure DevOps REST API更新工作项状态。 我有一个错误(400)。

这是我调用服务的代码:

string updateFeaturesStateBody = "
[
    {
        "op": "test",
        "path": "/rev",
        "value": "4"
    },
    {
        "op": "add",
        "path": "/fields/System.State",
        "value": "Blocked"
    },
    {
        "op": "add",
        "path": "/fields/System.Reason",
        "value": "State changed as part of PWR planning"
    },
    {
        "op": "add",
        "path": "/fields/System.History",
        "value": "State changed as part of PWR planning"
    }
]";

StringContent wibatchContent = new StringContent(updateFeaturesStateBody, Encoding.UTF8, "application/json");

HttpResponseMessage responseMessage = await HttpClient.PatchAsync($"/{Org}/_apis/wit/workitems/{ID}?api-version=6.0", wibatchContent);

我始终得到响应:状态代码:400,推理:“不良请求”

有人可以帮助我弄清楚问题是什么?

I am trying to write a code to update a work item state through Azure DevOps rest API.
I am getting an error (400).

This is my code to invoke the service:

string updateFeaturesStateBody = "
[
    {
        "op": "test",
        "path": "/rev",
        "value": "4"
    },
    {
        "op": "add",
        "path": "/fields/System.State",
        "value": "Blocked"
    },
    {
        "op": "add",
        "path": "/fields/System.Reason",
        "value": "State changed as part of PWR planning"
    },
    {
        "op": "add",
        "path": "/fields/System.History",
        "value": "State changed as part of PWR planning"
    }
]";

StringContent wibatchContent = new StringContent(updateFeaturesStateBody, Encoding.UTF8, "application/json");

HttpResponseMessage responseMessage = await HttpClient.PatchAsync(
quot;/{Org}/_apis/wit/workitems/{ID}?api-version=6.0", wibatchContent);

I always get the response: StatusCode: 400, ReasonPhrase: 'Bad Request'

Anyone can help me figure what is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友谊不毕业 2025-02-20 17:00:33

400错误的原因有很多,请在可能的情况下分享详细的错误消息。请确保您要修改的状态存在,并满足修改状态的先决条件。

从您的代码中我可以看出,您没有指定适当的标头。

此REST API具有对请求车身类型的要求,您必须指定它。

以下代码在我这边工作正常:

using RestSharp;
using System;

namespace ChangeStatus
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string OrgName = "<organization name>";
            string ProjectName = "<project name>";
            string workitem_id = "<workitem id>";
            string state_tochange = "<valid value of workitem>";
            string PAT = "<PAT token here>";
            var client = new RestClient("https://dev.azure.com/"+OrgName+"/"+ProjectName+"/_apis/wit/workitems/"+workitem_id+"?api-version=6.0");
            var request = new RestRequest("https://dev.azure.com/"+OrgName+"/"+ProjectName+"/_apis/wit/workitems/"+workitem_id+"?api-version=6.0", Method.Patch);
            request.AddHeader("Content-Type", "application/json-patch+json");
            request.AddHeader("Authorization", "Basic "+PAT);
            var body = @"
                " + "\n" +
                            @"    [{
                " + "\n" +
                            @"        ""op"": ""add"",
                " + "\n" +
                            @"        ""path"": ""/fields/System.State"",
                " + "\n" +
                            @"        ""value"": """+
                                                        $"{state_tochange}"+
                            @"""
                " + "\n" +
                            @"    }]";
            request.AddParameter("application/json-patch+json", body, ParameterType.RequestBody);


            var response = client.Execute(request);
            Console.WriteLine(response.Content);
        }
    }
}

There are many possible reasons for the 400 error, please share the detailed error message if possible. Please make sure that the state you want to modify exists and the preconditions for modifying the state are met.

What I can tell from your code is that you are not specifying the proper header.

This rest api has requirements for the request body type, you must specify it.

The below code works fine on my side:

using RestSharp;
using System;

namespace ChangeStatus
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string OrgName = "<organization name>";
            string ProjectName = "<project name>";
            string workitem_id = "<workitem id>";
            string state_tochange = "<valid value of workitem>";
            string PAT = "<PAT token here>";
            var client = new RestClient("https://dev.azure.com/"+OrgName+"/"+ProjectName+"/_apis/wit/workitems/"+workitem_id+"?api-version=6.0");
            var request = new RestRequest("https://dev.azure.com/"+OrgName+"/"+ProjectName+"/_apis/wit/workitems/"+workitem_id+"?api-version=6.0", Method.Patch);
            request.AddHeader("Content-Type", "application/json-patch+json");
            request.AddHeader("Authorization", "Basic "+PAT);
            var body = @"
                " + "\n" +
                            @"    [{
                " + "\n" +
                            @"        ""op"": ""add"",
                " + "\n" +
                            @"        ""path"": ""/fields/System.State"",
                " + "\n" +
                            @"        ""value"": """+
                                                        
quot;{state_tochange}"+
                            @"""
                " + "\n" +
                            @"    }]";
            request.AddParameter("application/json-patch+json", body, ParameterType.RequestBody);


            var response = client.Execute(request);
            Console.WriteLine(response.Content);
        }
    }
}
凉世弥音 2025-02-20 17:00:33

我最终了解了什么问题:

我必须更改以下行:

String content wibatchContent = new StringContent(updateFeatUressTateBody,Encoding.utf8,“ application/json”);

to以下行:

code> string string wibatchContent wibatchContent wibatchContent = new StringContent(updateFeatureStateBody,encoding.utf8, “应用程序/JSON-PATCH+JSON”);

I Finally understood what was the problem:

I had to change this line:

StringContent wibatchContent = new StringContent(updateFeaturesStateBody, Encoding.UTF8, "application/json");

to this line:

StringContent wibatchContent = new StringContent(updateFeaturesStateBody, Encoding.UTF8, "application/json-patch+json");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文