1

I am attempting to utilize the Azure DevOps REST API to create and/or update a work item in Boards to allow adding a Parent link. So far this is unsuccessful. and its creating an error...

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}

Here is code I have tried.

# 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
1
  • Does request work with Postman? If so check the Postman Control panel for entire request. Often errors like this are do to http headers missing. Postman will automatically add http headers while PS will not. The solution is to add the missing header found in Postman to the PS code. The error indicates the server is expecting serialized data. Commented Jul 18, 2024 at 23:45

1 Answer 1

0

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"You must pass a valid patch document in the body of the request.".

This issue is usually caused by the lack of [{}] in the Request body format. But in the PowerShell code, the request body should be correct.

The only issue I can see is that there are extra spaces in the Rest API url.

For example:

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

After I remove the additional spaces, the PowerShell script can work as expected.

Here is an example:

# 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

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.