简体   繁体   English

通过 API 创建 WorkItem 时出错 - Devops

[英]Error while creating a WorkItem via API - Devops

I'm trying to create a workitem via API, but im getting the following error:我正在尝试通过 API 创建一个工作项,但我收到以下错误:

{
  "innerException": null,
  "message": "You must pass a valid patch document in the body of the request.",
  "typeName": "Microsoft.VisualStudio.Services.Common.VssPropertyValidationException, Microsoft.VisualStudio.Services.Common",
  "typeKey": "VssPropertyValidationException",
  "errorCode": 0,
  "eventId": 3000
}

Code:代码:

public class Chamados
    {
        public async Task<string> CriaChamadoDevOps()
        {
            string organizacao = "xxx";
            string departamento = "xxx";
            string tipoWorkItem = "xxx";
            string authToken = "xxx";

            // Montando a Requisição
            string urlReq = "https://dev.azure.com/" + organizacao + "/" + departamento + "/_apis/wit/workitems/$" + tipoWorkItem + "?api-version=6.0";
            var client = new RestClient(urlReq);            
            var request = new RestRequest(urlReq, Method.Post);

            // Montando Headers
            request.AddHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", authToken))));            
            request.AddHeader("Content-Type", "application/json-patch+json; charset=utf-8");            

            var body = new JArray {
                new JObject {
                    { "op", "add" },
                    { "path", "/fields/System.Title" },
                    { "value", "Assunto Teste" }
                },
                new JObject {
                    { "op", "add" },
                    { "path", "/fields/System.State" },       
                    { "value", "To do" }
                },
                new JObject {
                    { "op", "add" },
                    { "path", "/fields/System.Description" },                    
                    { "value", "Descricao Teste" }
                },                
            };

            //request.AddBody(body);
            request.AddParameter("application/json-patch+json; charset=utf-8", body, ParameterType.RequestBody);            
            Console.WriteLine(body);            
            RestResponse response = await client.ExecuteAsync(request);
            dynamic resposta = JsonConvert.DeserializeObject(response.Content);

            return resposta.ToString();
        }
    }

When i test it via Postman, it works.当我通过 Postman 对其进行测试时,它可以工作。

This is how im sending the body to the request: (Output from Console.WriteLine(body);)这就是我将正文发送到请求的方式:(来自 Console.WriteLine(body) 的输出;)

[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "value": "Assunto Teste"
  },
  {
    "op": "add",
    "path": "/fields/System.State",
    "value": "To do"
  },
  {
    "op": "add",
    "path": "/fields/System.Description",
    "value": "Descricao Teste"
  }
]

And i've also tried replacing the "request.AddParameter()" with "request.AddBody()" method.而且我还尝试用“request.AddBody()”方法替换“request.AddParameter()”。

Maybe you can start by reading the docs: https://restsharp.dev/usage.html#request-body也许您可以从阅读文档开始: https ://restsharp.dev/usage.html#request-body

Then, you'd figure out that since you don't want to build strongly-typed request models and prefer using JObject , you need to handle the serialization yourself:然后,您会发现,由于您不想构建强类型请求模型并且更喜欢使用JObject ,因此您需要自己处理序列化:

request.AddStringBody(body.ToString(), "application/json-patch+json");

and that the docs tell you not to add content-type as the request header as it's the content header, and AddStringBody will do it.并且文档告诉您不要将 content-type 添加为请求标头,因为它是内容标头,并且AddStringBody会这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM