Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

PowerShell Confluence REST API Page Update

Mark DeBella October 5, 2017

I'm trying to update an already-existing page in Confluence using PowerShell's Invoke-WebRequest cmdlet. I've used this method many times in both getting and putting information into a series of different REST APIs including JIRA, and my GET requests work with Confluence too. So I know my usage of Invoke-WebRequest is correct, but have to assume there's something wrong with the "body" of my request.

Here's my code:

-------------------------------------------------------------------------------

$Credentials = Get-Credential
$MaxJsonLength = 67108864

$ConfluenceURL = "https://confluenceurl/rest/api/"
$ConfluencePageID = "123456789"
$Headers = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(($Credentials.UserName+":"+[System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($Credentials.Password)) )))}

$Call = "content/$($ConfluencePageID)?expand=body.storage,version,space,ancestors"
$CurrentConfluence = Invoke-WebRequest -Method GET -Headers $Headers -Uri ($ConfluenceURL + $Call)
$JSONSerial = New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer
$JSONSerial.MaxJsonLength = $MaxJsonLength
$CurrentConfluence = ($JSONSerial.DeserializeObject($CurrentConfluence))

$Body = @{
     "id" = ($CurrentConfluence.id)
     "type" = "page"
     "title" = ($CurrentConfluence.title)
     "version" = @{
          "number" = ($CurrentConfluence.version.number + 1)
     }
     "status" = "current"
     "space" = @{
          "id" = ($CurrentConfluence.space.id)
          "key" = ($CurrentConfluence.space.key)
          "name" = ($CurrentConfluence.space.name)
          "type" = ($CurrentConfluence.space.type)
     }
     "ancestors" = @{
          "id" = ($CurrentConfluence.ancestors.id)
          "type" = ($CurrentConfluence.ancestors.type)
          "status" = ($CurrentConfluence.ancestors.status)
          "title" = ($CurrentConfluence.ancestors.title)
     }
     "body" = @{
          "storage" = @{
               "value" = "This is a newly-posted Confluence page"
               "representation" = "editor"
          }
     }
}
$Body = $Body | ConvertTo-Json
$ContentType = "application/json"
$Call = "content/$($ConfluencePageID)"
$UpdateConfluence = Invoke-WebRequest -Method PUT -Uri ($ConfluenceURL + $Call) -Body $Body -ContentType $ContentType
$JSONSerial = New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer
$JSONSerial.MaxJsonLength = $MaxJsonLength
$UpdateConfluence = ($JSONSerial.DeserializeObject($UpdateConfluence))

-------------------------------------------------------------------------------

The first REST GET call I make with at the top works fine  ($CurrentConfluence), but I keep getting 400 Bad Request errors on my "$UpdateConfluence" line where attempt a PUT call.

I've played with the body section of my code as many ways as I can but can't seem to find the proper combination. Version aside (documented here: https://docs.atlassian.com/confluence/REST/latest-server/#content-update) are there any other requirements needed to perform an update? Really the ONLY thing I want to update is the body.storage.value field. I want to keep everything else the same. I have the other body code in my example because other posts on the web seem to include it.

2 answers

0 votes
Rasmus Hansen February 22, 2018

ancestors expects an array of objects, so in order for this to work you would have to it like this:

"ancestors" = @(
   @{"id" = '27820213'}
)

0 votes
Hasnae
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 21, 2017

Could it be that your are sending ancestors field as an object rather than an array ?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events