【问题标题】:Error while creating Work Item in Azure Devops using Rest API使用 Rest API 在 Azure Devops 中创建工作项时出错
【发布时间】:2021-03-19 10:08:39
【问题描述】:

当我尝试使用 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",
“内部异常”:空,
"message": "请求为方法类型 "POST" 指明 Content-Type 为 "application/json",但不受支持。此方法的有效内容类型为:application/json-patch+json。",
"typeName": "Microsoft.VisualStudio.Services.WebApi.VssRequestContentTypeNotSupportedException, Microsoft.VisualStudio.Services.WebApi",
"typeKey": "VssRequestContentTypeNotSupportedException",
“错误代码”:0,
“eventId”:3000

当我在 Postman 中尝试 Post 请求时,我遇到了同样的错误。

【问题讨论】:

  • 您是否仔细查看了错误消息和文档并尝试相应地调整您的代码? 此方法的有效内容类型为:application/json-patch+json。

标签: azure powershell rest azure-devops


【解决方案1】:

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

您必须将 Content Type 标头传递为 application/json-patch+json

修正后的命令是

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

Reference

【讨论】:

    【解决方案2】:

    同意 Satya V 和 Daniel。

    邮递员结果:

    Power 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
    

    结果:

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 1970-01-01
      • 2021-02-22
      • 2021-05-30
      • 2020-07-06
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多