使用DevOps API运行多参数Pipeline时遇JSON格式错误求助
Azure DevOps Pipeline API触发参数传递失败问题解决
问题场景
有一个包含两个参数的Azure DevOps Pipeline,尝试通过API触发运行时出现JSON解析错误,无法正确传递多个参数。
原PowerShell请求代码
$body = @" { "templateparameters": "{ \"environmentconnection\":\"avd-d-lab-p\"}", \"avdtype\": \"OntwikkelLow\" }", "definition": { "id": 96 }, } "@ Invoke-RestMethod -Uri "https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=7.0" -Method Post -Headers $AzureDevOpsAuthenicationHeader -ContentType "application/json" -body $body
Pipeline YAML定义
parameters: - name: environmentconnection displayName: Select your destination type: string default: avd-d-lab-n values: - avd-d-lab-n - avd-d-lab-p - avd-n-dev - avd-p-prod - name: avdtype type: string displayName: Select your AVD type values: - OntwikkelLow - OntwikkelMedium - OntwikkelHigh - TestLow - TestMedium - TestHigh
Pipeline详情
_links : @{self=; web=} url : https://dev.azure.com/{project}/15c1d992-70ab-4790-9650-04cd457af10b/_apis/pipelines/96?revision=2 id : 96 revision : 2 name : 3. AVD Create Sessionhost Keuzemenu folder : \AVD
错误信息
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF400898: An Internal Error Occurred. Activity Id: 5837e9f7-e6ca-4a3f-bff9-7a34ad2a20dc.","typeName":"Newtonsoft.Json.JsonReaderException, Newtonsoft.Json","typeKey":"JsonReaderException","errorCode":0,"eventId":0} At line:1 char:1 + Invoke-RestMethod -Uri "https://dev.azure.com/{organization}/{project} ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
错误原因
请求体中templateparameters字段的JSON格式转义完全错误:
- 该字段要求是字符串化的完整JSON对象,但原代码中括号配对错误,且转义后的字符串被提前截断,导致整个请求体JSON结构损坏,触发Newtonsoft.Json解析异常。
- 请求体末尾多了多余的逗号,进一步破坏JSON结构。
修正方案
推荐两种可靠的写法,避免手动转义出错:
方法1:用PowerShell哈希表自动转JSON
通过构建嵌套哈希表,再用ConvertTo-Json自动处理转义和格式,最不易出错:
# 构建参数哈希表 $params = @{ environmentconnection = "avd-d-lab-p" avdtype = "OntwikkelLow" } # 构建请求体哈希表 $bodyObject = @{ templateparameters = ($params | ConvertTo-Json -Compress) definition = @{ id = 96 } } # 转成最终JSON $body = $bodyObject | ConvertTo-Json Invoke-RestMethod -Uri "https://dev.azure.com/{organization}/{project}/_apis/pipelines/96/runs?api-version=7.0" -Method Post -Headers $AzureDevOpsAuthenicationHeader -ContentType "application/json" -Body $body
方法2:手动正确构造转义后的字符串
如果坚持手动写JSON字符串,需确保templateparameters是完整的转义JSON字符串,且请求体结构无语法错误:
$body = @" { "templateparameters": "{\"environmentconnection\":\"avd-d-lab-p\",\"avdtype\":\"OntwikkelLow\"}", "definition": { "id": 96 } } "@ Invoke-RestMethod -Uri "https://dev.azure.com/{organization}/{project}/_apis/pipelines/96/runs?api-version=7.0" -Method Post -Headers $AzureDevOpsAuthenicationHeader -ContentType "application/json" -Body $body
关键注意点
templateparameters的值必须是完整的字符串化JSON对象,所有内部双引号都要转义(\")- 请求体本身的JSON结构要严谨,不能有多余的逗号或未闭合的括号
内容的提问来源于stack exchange,提问作者Martin20
相关产品推荐
相关产品推荐

