hello all,
im trying to get the contents of existing page and update some values and post it again using powershell but im getting 400 bad request due to the HTML tags inside the storage values.
however i have tried to escape the HTML tags manually and it works. still i feel like this is not a standard solution because i know the contents of the current page and escaped the html tags manually, but incase if i want to update many pages where i do not know the contents inside the pages. the request will fail if there is a new HTML tags or something which needs to be escaped.
any suggestions to overcome the issue?
Code:
function CreateHeaders([string]$username, [string]$password) {
# Create a username:password pair
$credPair = "$($username):$($password)"
# Encode the pair to Base64 string
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))
# Create Header object and add authorization & KeyId to it
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic $encodedCredentials")
$headers.Add("keyid", "xxxx-xxxx-xxxxx-xxxxx-xxxxx-xxxxx")
$headers.Add("Content-Type","application/json")
#return the headers object
return $headers
}
# Add below line if you get the TLS error
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$headers = CreateHeaders -username "xxxxxxxxxxxx" -password "xxxxxxxxx"
$pageId = 123456789
$urlGet = "https://xxxx.xxx.xxx/v1.1/content/" + $pageId + "?expand=body.storage,version,title,space"
$urlPut = "https://xxx.xxx.xxx/v1.1/content/" + $pageId
#Invoke REST
$page = Invoke-RestMethod -Uri $urlGet -Method Get -Headers $headers
#test get output
#write-host $page
#Retreive Content Information from Page and Store them in Vars
$bodyContent = $page.body.storage.value
$newPageVersion = $page.version.number+1
$pageTitle = $page.title
$spaceKey = $page.space.key
Write-Host "Actual Body: " $bodyContent
Write-Host $newPageVersion
Write-Host $pageTitle
Write-Host $spaceKey
#Replace Content String here
$modifiedContent = $($bodyContent).Replace("try_to_replace","replaced_successfully")
$escapehtmltags = $($modifiedContent).Replace("/","\/")
$escapehtmltags1 = $($escapehtmltags).Replace("href=","href=\")
$escapehtmltags2 = $($escapehtmltags1).Replace("class=","class=\")
$escapehtmltags3 = $($escapehtmltags2).Replace('" ','\" ')
$escapehtmltags4 = $($escapehtmltags3).Replace('">','\">')
$escapehtmltags5 = $($escapehtmltags4).Replace('http://','https:\/\/')
Write-Host $escapehtmltags5
#Invoke PUT REST Call
$postParams = @"
{
"id":"$pageId",
"type":"page",
"title":"$pageTitle",
"space":{"key":"$spaceKey"},
"body":{
"storage":{
"value":"$escapehtmltags5",
"representation":"storage"
}
},
"version":{"number":$newPageVersion}
}
"@
Write-Host ""
Write-Host ""
Write-Host "After changing body contents: " $postParams
$res = Invoke-RestMethod -Uri $urlPut -Method PUT -Body $postParams -Headers $headers
Write-Output $res.StatusCode