PowerShell Confluence REST API Page Update Edited

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

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

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

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

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

381 views 21 10
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you