如何只更新正文请求中的一个字段?
how to update only one field in body request?
我正在尝试使用提供更新发布管道的描述 Rest API documentation。
我应该如何发送 'Put' 请求以更新字段?
我试过用sniffer手动修改捕获请求,看来我的代码完全一样。
$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
我收到一条错误消息:
the Id of the Release does not match the Id of the original release
resource. Make sure that you are trying to update the correct resource
最好的方法是使用相同的 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'
我正在尝试使用提供更新发布管道的描述 Rest API documentation。
我应该如何发送 'Put' 请求以更新字段?
我试过用sniffer手动修改捕获请求,看来我的代码完全一样。
$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
我收到一条错误消息:
the Id of the Release does not match the Id of the original release resource. Make sure that you are trying to update the correct resource
最好的方法是使用相同的 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'