Azure DevOps:尝试使用API更新工作项状态时遇到错误
我正在尝试编写一个代码来通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
400错误的原因有很多,请在可能的情况下分享详细的错误消息。请确保您要修改的状态存在,并满足修改状态的先决条件。
从您的代码中我可以看出,您没有指定适当的标头。
此REST API具有对请求车身类型的要求,您必须指定它。
以下代码在我这边工作正常:
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:
我最终了解了什么问题:
我必须更改以下行:
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");