[英]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. 我尝试了多种方法来解决此错误,我从我的VSTS帐户创建了PAT,并将其包含在脚本中。 However, the call to the REST API returns saying "The remote server returned an error: (400) Bad Request". 但是,对REST API的调用返回说“远程服务器返回错误:(400)错误的请求”。
Using the same PAT gets me the information from VSTS using the GET method but it is not creating a work item. 使用相同的PAT可以使用GET方法从VSTS中获取信息,但它不会创建工作项。
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 正文从CSV获取其值并存储在$ 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如下
$Uri = " https://[xxxx].visualstudio.com/InfraEng/_apis/wit/workitems/ `$product backlog item?api-version=1.0" $ 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 当我尝试触发Invoke-RestMethod时捕获响应时,得到以下信息
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; IsMutuallyAuthenticated:错误的Cookie:{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-0000-000000000000%22%7D}标头:{Pragma,X-TFS-ProcessId,Strict-Transport-Security,ActivityId ...} SupportsHeaders:真实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 charset = utf-8 CharacterSet:utf-8服务器: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方法: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. 我希望您可能会收到其他错误,特别是如果您能够在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. 我认为您的问题出在$Body
但这不包含在问题中。
Check out this page - https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/create?view=azure-devops-rest-5.0 签出此页面-https://docs.microsoft.com/zh-CN/rest/api/azure/devops/wit/work%20items/create?view=azure - devops - rest - 5.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.