Azure DevOps REST API 父关系链接到现有工作项错误

问题描述 投票:0回答:1

我正在尝试利用 Azure DevOps REST API 在面板中创建和/或更新工作项以允许添加父链接。 到目前为止,这是不成功的。它创建了一个错误...

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"您必须在主体中传递有效的补丁文档 请求。","typeName":"Microsoft.VisualStudio.Services.Common.VssPropertyValidationException, Microsoft.VisualStudio.Services.Common","typeKey":"VssPropertyValidationException","errorCode":0,"eventId":3000}

这是我尝试过的代码。

# Define your organization, project, PAT (Personal Access Token), and work item IDs
$organization = "Org"
$project = "Test%20Project"
$token = "PAT"
$parentWorkItemId = "7787"
$childWorkItemId = "8678"

$ContentType = "application/json-patch+json"

# Construct the URI for the REST API call
$uri = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/$childWorkItemId/?api-   version=7.2-preview.3"
echo $uri

# Create the JSON body for adding a parent link
$body = @"
[
    {
        "op": "add",
        "path": "/relations/-",
        "value": {
            "rel": "System.LinkTypes.Hierarchy-Reverse",
            "url": "https://dev.azure.com/$organization/_apis/wit/workItems/$parentWorkItemId"
        }
    }
 ]
"@

# Set the header with the PAT for authentication
$headers = @{
    Authorization = "Basic " +    [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($token)"))
 }

# Make the PATCH request to add the parent link
Invoke-RestMethod -Uri $uri -Method Patch -Body $body -ContentType $ContentType -Headers $headers
azure powershell devops
1个回答
0
投票

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"您必须在请求正文中传递有效的补丁文档。"。

此问题通常是由于请求正文格式中缺少

[{}]
导致的。但在 PowerShell 代码中,请求正文应该是正确的。

我看到的唯一问题是 Rest API url 中有多余的空格。

例如:

$uri = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/$childWorkItemId/?api-   version=7.2-preview.3"

删除多余的空格后,PowerShell 脚本可以按预期工作。

这是一个例子:

# Define your organization, project, PAT (Personal Access Token), and work item IDs
$organization = "ORG"
$project = "PROJECT"
$token = "PAT"
$parentWorkItemId = "1421"
$childWorkItemId = "1420"

$ContentType = "application/json-patch+json"

# Construct the URI for the REST API call
$uri = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/$childWorkItemId/?api-version=7.2-preview.3"
echo $uri

# Create the JSON body for adding a parent link
$body = @"
[
    {
        "op": "add",
        "path": "/relations/-",
        "value": {
            "rel": "System.LinkTypes.Hierarchy-Reverse",
            "url": "https://dev.azure.com/$organization/_apis/wit/workItems/$parentWorkItemId"
        }
    }
 ]
"@

# Set the header with the PAT for authentication
$headers = @{
    Authorization = "Basic " +    [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($token)"))
 }

# Make the PATCH request to add the parent link
Invoke-RestMethod -Uri $uri -Method Patch -Body $body -ContentType $ContentType -Headers $headers

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.