【问题标题】:Azure DevOps how to edit Wiki page via REST APIAzure DevOps 如何通过 REST API 编辑 Wiki 页面
【发布时间】:2019-07-16 11:36:57
【问题描述】:

我想通过 REST API (Azure DevOps Server 2019.0.1) 编辑 Azure DevOps wiki 页面。

当我运行这个 PowerShell 脚本时:

#VARIABLES
$api = "api-version=5.0"
$root = "http://136.202.18.216:8070/Samples"
$personalToken = "uwawlzqp6j7i1nd5dasspwkwp63tr2w2sxb5563zrla2bivynbza"

#AUTHORIZATION
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$headers = @{
    "Authorization" = ('Basic {0}' -f $token)
    "If-Match"      = '*'
}

#PROJECT VARIABLES
$project = "Framework%20A"
$pageToUpdate = "/Seite2"

#BODY
$body = @"
    {
        "content": "Hello"
    }
"@

#GET
$url = "$root/$project/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pages?version=wikiMaster&path=$pageToUpdate&includeContent=True&$api"
$content = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $headers
Write-Host "$($content.path) = $($content.content)" -ForegroundColor Yellow

#PUT
$url = "$root/$project/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pages?version=wikiMaster&path=$pageToUpdate&$api"
$update = Invoke-RestMethod -Uri $url -Method Put -ContentType "application/json" -Headers $header -Body $body -Verbose
Write-Host $update.content -ForegroundColor Yellow
exit(0)

控制台输出为:

/Seite2 = Seite 2 Content
AUSFÜHRLICH: PUT http://136.202.18.216:8070/Samples/Framework A/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pages?version=wikiMaster&path=/Seite2&api-version=5.0 with -1-byte payload
Invoke-RestMethod: {"$ id": "1", "innerException": null, "message": "The required \" IfMatch \ "header specified in the request is an invalid page version Version of the
Wiki page as \ "IfMatch \" header for the request. \ R \ nParameterName: IfMatch "," typeName ":" Microsoft.TeamFoundation.SourceControl.WebServer.InvalidArgumentValueException,
Microsoft.TeamFoundation.SourceControl.WebServer "," TypeKey ":" InvalidArgumentValueException "," error code ": 0," eventId ": 0}
In C:\Users\mkober\Desktop\Azure DevOps Skripte (Bearbeitung)\WikiAPI.ps1:34 Zeichen:11
+ $update = Invoke-RestMethod -Uri $url -Method Put -ContentType "appli ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

第一行/Seite2 = Seite 2 Content 是来自我要编辑的页面的成功get 请求的结果。 put 请求发生错误。我在这里做错了什么?

更新:(工作示例)

#VARIABLES
$api = "api-version=5.0"
$root = "http://136.202.18.216:8070/Samples"
$personalToken = "uwawlzqp6j7i1nd5dasspwkwp63tr2w2sxb5563zrla2bivynbza"

#AUTHORIZATION
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$headers = @{
    "Authorization" = ('Basic {0}' -f $token)
    "If-Match"      = '{0}'
}

#PROJECT VARIABLES
$project = "Framework%20A"
$pageToUpdate = "/Seite2"

#BODY
$body = @"
    {
        "content": "Hello"
    }
"@

#---------------------------------------------------------------------------------------------------------------------------
#GET
$url = "$root/$project/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pages?version=wikiMaster&path=$pageToUpdate&includeContent=True&$api"
$content = Invoke-WebRequest -Uri $url -Method Get -ContentType "application/json" -Headers $headers
$etag = $content.Headers.ETag
$headers.'If-Match' = $headers.'If-Match' -f $etag
Write-Host "$($content.path) = $($content.content)" -ForegroundColor Yellow

#PUT
$url = "$root/$project/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pages?version=wikiMaster&path=$pageToUpdate&$api"
$update = Invoke-RestMethod -Uri $url -Method Put -ContentType "application/json" -Headers $headers -Body $body -Verbose
Write-Host $update.content -ForegroundColor Yellow
exit(0)
#---------------------------------------------------------------------------------------------------------------------------

【问题讨论】:

    标签: azure-devops azure-devops-rest-api azure-devops-server-2019


    【解决方案1】:

    If-Match 标头中不能只放'*',您需要将页面的ETag 放在那里。

    你是怎么得到它的?在你的第一个GET 调用中使用Invoke-WebRequest(代替Invoke-RestMethod),现在在响应中你也会得到标题响应并且ETag 存在:

    $page = Invoke-WebRequest -Uri $url ...........
    $etag = $page.Headers.ETag
    $headers = @{
        "Authorizaion = ....."
        "If-Match" = $etag
    }
    

    现在第二个Invoke-RestMethod 将用于更新页面。

    【讨论】:

    • 非常感谢 Shayki,我用工作示例更新了我的问题。向您问好。
    猜你喜欢
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多