简体   繁体   中英

Bad Request error while trying to create a work item in VSTS using Powershell

I have tried many ways to get around this error, I have created a PAT from VSTS account of mine and included this in the script. However, the call to the REST API returns saying "The remote server returned an error: (400) Bad Request".

Using the same PAT gets me the information from VSTS using the GET method but it is not creating a work item.

I'm providing the authentication by following steps

$Creds = [Text.Encoding]::ASCII.GetBytes(":$Token")
$Creds = [System.Convert]::ToBase64String($Creds)
$Headers = @{
    Authorization = ("Basic {0}" -f $Creds)
}

and passing the rest by following steps

Invoke-RestMethod -Uri $Uri -Method POST -Headers $Headers -Body $Body -ContentType $ContentType

Body gets its values from a CSV and stored in $values

foreach ($value in $values)
{
   $PBIName = $value.Name
   $Resource = $value.Resource
   $Body        = "[
            {
                `"op`": `"add`",
    `"path`": `"/fields/System.Title`",
    `"value`": `"$($PBIName)`"
            }
            {
                `"op`": `"add`",
    `"path`": `"/fields/System.AreaPath`",
    `"value`": `"InfraEng\DCO`"
            }
            {
          `"op`": `"add`",
    `"path`": `"/fields/System.IterationPath`",
    `"value`": `"InfraEng`"
            }
            {
                     `"op`": `"add`",
    `"path`": `"/fields/System.AssignedTo`",
    `"value`": `"$($Resource)`"
            }]"

| ConvertTo-Json

and URI is as follows

$Uri = " https://[xxxx].visualstudio.com/InfraEng/_apis/wit/workitems/ `$product backlog item?api-version=1.0"

when I try catching the response when I trigger Invoke-RestMethod, I get the following

IsMutuallyAuthenticated : False Cookies : {VstsSession=%7B%22PersistentSessionId%22%3A%2248171d3c-4c0f-413f-9143-59e6e50047c3%22%2C%22PendingAuthenticationSessionId%22%3A%22 00000000-0000-0000-0000-000000000000%22%2C%22CurrentAuthenticationSessionId%22%3A%2200000000-0000-0000-0000-000000000000%22%7D} Headers : {Pragma, X-TFS-ProcessId, Strict-Transport-Security, ActivityId...} SupportsHeaders : True ContentLength : 373 ContentEncoding : ContentType : application/json; charset=utf-8 CharacterSet : utf-8 Server : LastModified : 1/9/2019 9:35:03 AM StatusCode : BadRequest StatusDescription : Bad Request ProtocolVersion : 1.1 ResponseUri : https://[xxxx].visualstudio.com/InfraEng/_apis/wit/workitems/ $product backlog item?api-version=1.0 Method : POST IsFromCache : False

I don't necessarily think it is a problem with your Authorization. I'd expect you to probably get a different error, especially if you are able to use the header on a GET.

This works for me:

$Cred = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$pat"))
$Headers = @{
    Authorization = ("Basic {0}" -f $Cred)
}

Invoke-RestMethod -Uri 'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$Test Case?api-version=4.1' -Method PATCH -Header $Headers -Body '[{"op": "add","path": "/fields/System.Title","from": null,"value": "Sample test case"}]' -ContentType 'application/json-patch+json'

I think your issue is with the $Body but that is not included in the question.

Check out this page - https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/create?view=azure-devops-rest-5.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM