0 votes
in Programming by (400 points)

When I try to execute the following code I'll get an Error:

{"$id":"1","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}

My Code with the function to connect to the DevOps API and change the state of a workitem. As you can see I tried 2 versions of getting a json header. So how can I get around this error?

{ string testToken = "TOKEN";

public void GenerateRequest()
{
    StartCoroutine(DevOpsPatchRequestAsync());
}
public IEnumerator DevOpsPatchRequestAsync()
{
    // Create request URL string
    string url = $"https://company.com/company/company/_apis/wit/workitems/ID?api-version=5.1";

    //Create request body
      Dictionary<string, string> requestBody = new Dictionary<string, string>()
              {
                  {"op", "add" },
                  {"path", "/fields/System.State"},
                  {"value", "Closed" }
              };

    // Serialize body as a Json string
    string requestBodyString = JsonConvert.SerializeObject(requestBody);
       // Convert Json body string into a byte array
       byte[] requestBodyData = System.Text.Encoding.UTF8.GetBytes(requestBodyString);

    var bodyData = "{ 'op': 'add','path': '/fields/System.State','value': 'Closed',  }";
    var postData = System.Text.Encoding.UTF8.GetBytes(bodyData);

    // Create new UnityWebRequest, pass on our url and body as a byte array
    UnityWebRequest webRequest = UnityWebRequest.Put(url, postData);
    // Specify that our method is of type 'patch'
    webRequest.method = "PATCH";

    // Set request headers i.e. conent type, authorization etc

    webRequest.SetRequestHeader("Content-Type", "application/json-patch+json");

    webRequest.SetRequestHeader("Data-Type", "application/json-patch+json");

    webRequest.SetRequestHeader("Authorization", "Basic" + " " + testToken);

    
    webRequest.SetRequestHeader("Content-length", (postData.Length.ToString()));

    // Set the default download buffer
    webRequest.downloadHandler = new DownloadHandlerBuffer();

    // Send the request itself
    yield return webRequest.SendWebRequest();

    Debug.Log(webRequest.downloadHandler.text);
}

}

Please log in or register to answer this question.

...