【问题标题】:Azure DevOps Server Invoke-RestMethod Error: No API version provided for the "PUT" requestAzure DevOps 服务器 Invoke-RestMethod 错误:没有为“PUT”请求提供 API 版本
【发布时间】:2019-07-09 15:48:12
【问题描述】:

基于来自 Etienne Tremblay 的 PowerShell example,我尝试将数据传递到 Azure DevOps Server (2019.0.1) REST API。

在这个例子中我想修改一个项目的描述文本(只是为了测试):

# DEMO 5 Update an environement build variable
Write-Host "Demo 5"
$projects.value | ForEach-Object {
    $project = $_.name
    $buildManagementAreaId = "efc2f575-36ef-48e9-b672-0c6fb4a48ac5"
    $tfsBaseUrl = GetUrl -orgUrl $orgUrl -header $header -AreaId $buildManagementAreaId

    # https://docs.microsoft.com/en-us/rest/api/azure/devops/build/definitions/list?view=azure-devops-rest-5.0
    $relDefUrl = "$tfsBaseUrl$project/_apis/build/definitions?api-version=5.0"
    Write-Host "Requesting URL: $relDefUrl" -ForegroundColor Yellow
    $result = Invoke-RestMethod $relDefUrl -Method Get -ContentType "application/json" -Headers $header
    $relDefs = $result.value

    if($relDefs.count -gt 0){
        Write-Host "$project $($relDefs.count) build def founds" -ForegroundColor Blue
        $relDefs | ForEach-Object {

            $relDef = $_
            $relDefExpanded = Invoke-RestMethod "$($relDef.url)?`$Expand=Environments&api-version=5.0" -Method Get -ContentType "application/json" -Headers $header
            $relDefExpanded.project.description = "Hallo!!!"

            $body = $relDefExpanded | ConvertTo-Json -Depth 100 -Compress
            $body = [System.Text.Encoding]::UTF8.GetBytes($body)

            #ERROR OCCURS HERE
            $updateResult = Invoke-RestMethod "$($relDef.url)?api-version=5.0" -Method Put -ContentType "application/json" -body $body -Headers $header 
            Write-host "Variable value after: $($updateResult.project.description)" -ForegroundColor Green
        }
    }
}

提示:

  • $projects 之前在脚本中定义(demo1)
  • 原始脚本更改变量而不是描述(两个版本都不起作用)

错误发生在:

$updateResult = Invoke-RestMethod "$($relDef.url)?api-version=5.0" -Method Put -ContentType "application/json" -body $body -Headers $header 

带有信息:

Invoke-RestMethod: {"$ id": "1", "innerException": null, "message": "No API version provided for the" PUT "request The version must either be part of the" Accept "header (eg
\ "application / json; api-version = 1.0 \") or as a query parameter (for example, "? api-version = 1.0")
. Will "," type name ":" Microsoft.VisualStudio.Services.WebApi.VssVersionNotSpecifiedException,
Microsoft.VisualStudio.Services.WebApi","typeKey":"VssVersionNotSpecifiedException","errorCode":0,"eventId":3000}
In C:\Users\mkober\Desktop\Azure DevOps Console\WriteAPI.ps1:75 Zeichen:29
+ ... ateResult = Invoke-RestMethod "$($relDef.url)?api-version=5.0" -Metho ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

最终到达网址:

http://138.202.18.216:8070/Samples/19e02b27-74ab-40dd-a519-ece38fafa870/_apis/build/Definitions/11?revision=5?api-version=5.0

【问题讨论】:

    标签: powershell azure-devops azure-devops-rest-api


    【解决方案1】:

    你只需要修复网址,将api-version之前的?更改为&

    ?revision=5&api-version=5.0
    

    revisionapi-version 是参数,如果你只有一个参数,你使用?,但是当你想使用更多时,你需要在它们后面加上&

    【讨论】:

    • 你是对的 Invoke-RestMethod 接受固定的网址。非常感谢!但是$updateResult 不会接管$body 内部的更改。之后的结果将与之前相同。难道就不能这样改项目描述吗?
    • 应该是可以的。尝试$body = relDefExpanded | CovnertTo-Json -Depth 10 并删除 (encoding etc.) 之后的行
    • 没有encodingInvoke-RestMethod 会引发错误:Invoke-RestMethod: {"$ id": "1", "innerException": null, "message": "This request expects an object in the request body, but the specified data could not be deserialized.", "TypeName": "Microsoft.TeamFoundation.Build.WebApi.RequestContentException, Microsoft.TeamFoundation.Build2.WebApi "," TypeKey ":" Request Content Exception "," error code ": 0," eventId ": 3000}
    • @MarTin 这是有线的,我成功更新了没有编码的构建定义。
    • 我明白这一点。不幸的是,我与 azure devops 的整个工作都是有线的。我只运行我从 Etienne 发布的示例。是否有可能重现这个例子?
    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 2017-08-20
    • 2021-02-22
    • 1970-01-01
    • 2020-04-16
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多