簡體   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