繁体   English   中英

使用 Rest API 在 Azure Devops 中创建工作项时出错

[英]Error while creating Work Item in Azure Devops using Rest API

当我尝试使用 Rest API 在 Azure Devops 中创建工作项时,出现以下错误。

这是我正在使用的 PowerShell 脚本

$body ='[{"op": "add", "path": "/fields/System.Title", "from": null, "value": "Initiative Code"}]' | ConvertTo-Json
$body  = $body | ConvertFrom-Json
$supportAreaUri = 'https://MyOrganization.visualstudio.com/TestTeam1/_apis/wit/workitems/$Initiative?api-version=6.0'
$supportAreaUri = [uri]::EscapeUriString($supportAreaUri) 
$supportArea = Invoke-RestMethod -Method Post -Uri $supportAreaUri -Headers $Header -ContentType application/json -Body $body

运行代码时出现此错误:

"$id": "1",
“内部异常”:null,
"message": "请求指示方法类型 "POST" 的 Content-Type 为 "application/json",不支持此方法。此方法的有效内容类型为:application/json-patch+json。",
"typeName": "Microsoft.VisualStudio.Services.WebApi.VssRequestContentTypeNotSupportedException, Microsoft.VisualStudio.Services.WebApi",
"typeKey": "VssRequestContentTypeNotSupportedException",
“错误代码”:0,
“事件ID”:3000

当我尝试使用 Postman 作为Post请求时,我遇到了同样的错误。

查看错误消息,您似乎正在传递内容类型:“application/json”

您必须将内容类型 header 传递为application/json-patch+json

更正的命令将是

Invoke-RestMethod -Method Post -Uri $supportAreaUri -Headers $Header -ContentType "application/json-patch+json" -Body $body

在此处输入图像描述

参考

同意 Satya V 和 Daniel 的观点。

Postman 结果:

在此处输入图像描述

电源 shell 脚本

$connectionToken="{PAT}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$Url = "https://dev.azure.com/{Org name}/{Project name}/_apis/wit/workitems/"+"$"+"{Work item type}?api-version=6.0"  
$Body = @"
[
 {
 "op": "add",
 "path": "/fields/System.Title",
 "from": null,
 "value": "Sample Bug Test"
 }
]
"@
$Result = Invoke-RestMethod -Uri $Url -ContentType "application/json-patch+json" -Body $Body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST
write-host $Result.id

结果:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM