0
votes

I'm trying to add some info to work item history from PowerShell.

Sending for example json

[
{
    "path":  "/fields/System.History",
    "op":  "add",
    "value":  "Created by:\r\nRené Spišák\r\n\r\nTitle:\r\nPull request\r\n\r\nDescription:\r\nTest description"
}
]

to url

https://XXX.visualstudio.com/XXX/_apis/wit/workitems/14611?api-version=5.1

using

Invoke-RestMethod $workItemUrl -ContentType "application/json-patch+json" -Method "PATCH" -Headers $headers -Body $jsonBody

ends with error

Invoke-RestMethod : {"$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}
At C:\agent\_work\_temp\afb9aee6-21d2-4ea1-b455-916baee6d74b.ps1:127 char:29
+ ... mResponse = Invoke-RestMethod $workItemUrl -ContentType "application/ ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc 
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Sending the same json to the same url using Postman works fine.

What is wrong with this api? What is "valid patch document"?

How can I send formatted (new lines, html, etc.) text to work items api?
https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/update?view=azure-devops-rest-5.1

EDIT:
Just for clarification - problem is only in value field in json. Other part of script works fine. I can post this JSON to WIT Api using Postman. All works. But using Invoke-RestMethod fails.

So maybe PowerShell handles sending Json in different way? Escaping? Charset?

SOLVED:
Incorrect ContentType, must be "application/json-patch+json; charset=utf-8"

1
Show your script.Daniel Mann

1 Answers

1
votes

It seems powershell doesn't recognize characters "René Spišák", try other characters.