Home > Mobile >  Getting 'You must pass a valid patch document ...' when trying to update work item using P
Getting 'You must pass a valid patch document ...' when trying to update work item using P

Time:06-10

I'm creating a Powershell script that will update a specific field in a work item using the Azure DevOps REST API. I have the request body as a hashtable and then convert to JSON when making the request.

I get this error when making the request:

 {"$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 my Powershell script:

$url = 'https://dev.azure.com/orgname/projectname/_apis/wit/workitems/9999?api-version=7.1-preview.3'

$Token = 'PAT generated in DevOps'

$body = @(
    @{
        op = "add"
        path = "/fields/Microsoft.VSTS.Build.IntegrationBuild"
        value = "v.09-09-1999"
    }
)

$AzureAuthHeader = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $Token)))

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $AzureAuthHeader))
$headers.Add("Content-Type", "application/json-patch json")

$response = Invoke-RestMethod -Uri $url -Method PATCH -Headers $headers -Body ($body|ConvertTo-Json -Depth 2)

I'm converting the hashtable to JSON and the request body format is following this example. I'm not sure what is the problem.

CodePudding user response:

Test with the same Powershell script and get the same issue.

When you use the format $body|ConvertTo-Json -Depth 2, the json value is as follow:

{
    "path":  "/fields/Microsoft.VSTS.Build.IntegrationBuild",
    "op":  "add",
    "value":  "v.09-09-1999"
}

The body is not same as the example in the offcial doc.

The cause of this issue is that if you have an array object with just one item piping to ConvertTo-Json will treat it as a single object (not an array).

The expected format is an array.

For example:

[
    {
        "path":  "/fields/Microsoft.VSTS.Build.IntegrationBuild",
        "op":  "add",
        "value":  "v.09-09-1999"
    }
]

To solve this issue, you can change to use the following format:

ConvertTo-Json -InputObject $body

Here is the PowerShell example:

$url = 'https://dev.azure.com/orgname/projectname/_apis/wit/workitems/9999?api-version=7.1-preview.3'

$Token = 'PAT generated in DevOps'

$body = @(
    @{
        op = "add"
        path = "/fields/Microsoft.VSTS.Build.IntegrationBuild"
        value = "v.09-09-1999"
    }
)

$AzureAuthHeader = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $Token)))

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $AzureAuthHeader))
$headers.Add("Content-Type", "application/json-patch json")

$response = Invoke-RestMethod -Uri $url -Method PATCH -Headers $headers -Body (ConvertTo-Json -InputObject $body)
  • Related