【问题标题】:how to update only one field in body request?如何只更新正文请求中的一个字段?
【发布时间】:2019-08-05 10:01:39
【问题描述】:

我正在尝试使用提供 Rest API documentation 更新发布管道的描述。

我应该如何发送“Put”请求以更新字段?

我尝试使用嗅探器手动修改和捕获请求,看来我的代码完全一样。

$theBody = ConvertTo-Json @{description='Added a description'}


$instance = "tfs:8080"
$collection = "Collection"
$project = "myProject"
$releaseID = 1234
$apiVersion = "?api-version=4.1-preview.6"
$URI =  "http://"+$instance+"/"+$collection+"/"+$project+"/_apis/release/releases/"+$releaseID+$apiVersion


$res= Invoke-RestMethod -Method Put  -Uri $URI  -UseDefaultCredentials -Body $theBody  -ContentType 'application/json'
write-output $res

我收到一条错误消息:

版本的 ID 与原始版本的 ID 不匹配 资源。确保您正在尝试更新正确的资源

【问题讨论】:

  • 尝试使用GET https://{instance}/{collection}/{project}/_apis/release/releases?api-version=5.0 列出您的所有发布管道,并将您的 id 与响应 id 进行比较。和你用的一样吗?
  • @MarTin 嘿,我可以使用上面的代码执行 GET 甚至是 Patch 请求,但似乎在 Put 请求中,无法完成单个变量更新。

标签: powershell azure-devops azure-pipelines-release-pipeline azure-devops-rest-api


【解决方案1】:

最好的方法是使用相同的 URL 但使用 Get 方法(并且没有正文)获取版本:

$release = Invoke-RestMethod -Method Get-Uri $URI -UseDefaultCredentials -ContentType 'application/json'

然后修改描述:

$release.description = "Added a description"

将发布转换为 JSON:

$theBody = $release | ConvertTo-Json -Depth 10

然后做Put:

$res = Invoke-RestMethod -Method Put -Uri $URI -UseDefaultCredentials -Body $theBody  -ContentType 'application/json'

【讨论】:

  • 嘿,这是一个很好的解决方案,而且效果很好。谢谢。
猜你喜欢
  • 1970-01-01
  • 2019-11-11
  • 2018-05-25
  • 2013-07-27
  • 2023-03-15
  • 2021-08-07
  • 2022-08-24
  • 2017-02-10
  • 2020-02-28
相关资源
最近更新 更多