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
Community showcase
Published Mar 12, 2019 in Confluence

Confluence Admin Certification now $150 for Community Members

More and more people are building their careers with Atlassian, and we want you to be at the front of this wave! Important Dates Start the Certification Prep Course by 2 April 2019 Take your e...

1,165 views 2 13
Read article

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