Search code examples
azurerestsharp

RestSharp PATCH API call to update Azure Devops work item throwing BAD Request - "You must pass a valid patch document in the body of the request."


I am using RestSharp v107.

I want to update Iteration Path of a test case. I am able to update it with Postman but using RestSharp I am getting "BAD Request" - "You must pass a valid patch document in the body of the request."

Azure Devops Update Work Item Rest API

My Code:

        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;

I have tried with "AddJSONBody", ExecuteAsync - with respective changes. Still no help.


Solution

  • Sometimes it helps to just read the documentation.

    All you needed to do is:

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