Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Post HTML page in body parameter using PowerShell and Confluence REST API

Jan Pauli
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 17, 2020

Hi,

This is my whole scenario: Export OneNote pages via powershell, then upload them via REST API into Confluence.

The export is fine and I´m getting functional html-pages. But how do I upload them into confluence without external solutions like plugins or server-addons?

My current script (creating pages is working with e.g. <p> test content </p>):

#Variables
$Credentials = GET-Credential
$ConfluenceURL = "https://<domain>.com/rest/api/content/"

$pageContentHTML = GET-Content C:\<directory>\TestFile.htm

#Where the page gets created
$spaceKey = '<key>' #Space Key
$parentId = "<parentID>" #ID of parent page
$pageTitle = 'A first test page'

#Content
$pageContent = $pageContentHTML

#Generate post request as PowerShell objects
$post = @{
type = 'page'
"ancestors" = @(
@{"id" = $parentId}
)
title = $pageTitle
space = @{ key = $spaceKey}
body = @{
storage = @{
value = '$pageContent'
representation = 'storage'
}
}
}

#Header for authentification
$header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(($Credentials.UserName+":"+[System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($Credentials.Password)) )))}

#Convert post to json
$json = ConvertTo-Json $post

#Post request to Confluence
try {
Invoke-RestMethod -Uri $ConfluenceURL -Method POST -ContentType "application/json" -Body $json -Headers $header
} catch {$_.Exception.Response }

 

 

Thank you

so long

Jan

1 answer

1 accepted

0 votes
Answer accepted
Jan Pauli
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 20, 2020

Meanwhile I managed it with parsing through the html-file and find my content with help of the tags. The end result is one big string, which is passed as a variable into "value" in the post request. Perhaps not that beautiful, but it works.

$doc = New-Object -Com "HTMLFile"
$pagePath = "C:\<path>\file.html"
$doc.IHTMLDocument2_write($(Get-Content $pagePath -raw))


foreach ($tag in $doc.all) {
if (($tag.innerHTML -like $tag.InnerText) -and ($tag.InnerText)) {
       .... <code> ....

}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events