Forums

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

PowerShell Confluence REST API Page Update

Mark DeBella
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!
October 5, 2017 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

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 edited

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
atlassian, confluence, marketing templates, confluence templates, marketing project management, content strategy, campaign planning, marketing collaboration, confluence for marketers, atlassian learning, marketing workflows, confluence best practices

5 must-use templates for Marketing teams (from a Confluence Marketer!) 👩🏻‍🎨

Who doesn’t love a good template? We sure do! Check out our top template picks for Marketing teams who want to streamline their processes, enhance collaboration, and take their marketing to new heights.

Read more 📚
AUG Leaders

Atlassian Community Events