0

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

8
  • 1
    This is a problem with the body of your request, not your authentication. Update your question to include more details. Commented Jan 9, 2019 at 4:43
  • If you are going to source data from a CSV/Excel, have you considered just using the Excel extension to bulk add or modify? Commented Jan 9, 2019 at 14:56
  • @DanielMann thanks for the response, I have included the body in the question. Commented Jan 9, 2019 at 15:02
  • @Matt I actually haven't tried that, let me give it a try. Commented Jan 9, 2019 at 15:04
  • @ShadRahil You're creating JSON by doing string concatenation, then pushing it through ConvertTo-Json, which makes no sense. Create a JSON body by making an appropriately-shaped associative array and converting that to JSON. Also, don't use api-verison=1.0, use a more current API version. Commented Jan 9, 2019 at 15:17

2 Answers 2

0

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'
Sign up to request clarification or add additional context in comments.

Comments

-1

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

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

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.