0

I'm trying to create a workitem via API, but im getting the following error:

{
  "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.

This is how im sending the body to the request: (Output from 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.

1 Answers1

0

Maybe you can start by reading the docs: 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:

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.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83