我想从PowerShell中添加一些信息到工作项目历史记录中。
例如发送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"
}
]
到网址
https://XXX.visualstudio.com/XXX/_apis/wit/workitems/14611?api-version=5.1
使用
Invoke-RestMethod $workItemUrl -ContentType "application/json-patch+json" -Method "PATCH" -Headers $headers -Body $jsonBody
以错误结束
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
使用以下方法发送相同的json到相同的url中 Postman
工作正常。
这个api有什么问题?什么是 "有效的补丁文件"?
如何将格式化(新行、html等)的文本发送到工作项目api上?https:/docs.microsoft.comen-usrestapiazuredevopswitwork%20itemsupdate?view=azure-devops-rest-5.1。
EDIT: 只是为了澄清--问题只出现在 value
字段。脚本的其他部分工作正常。我可以将这个JSON发布到WIT Api上,使用 Postman
. 所有的工作。但使用 Invoke-RestMethod
失败。
所以可能PowerShell处理发送Json的方式不同?Escaping?Charset?
解决:不正确 ContentType
,必须是 "applicationjson-patch+json; charset=utf-8"
似乎powershell不能识别字符 "René Spišák",试试其他字符。