Confluence Cloud API
Update confluence doc
$headers = @{
'Accept' = 'application/json'
'Content-Type' = 'application/json'
'Authorization' = ("Basic {0}" -f ([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f 'alexandrm@rabota.ua', '**************')))))
}
$document = Invoke-RestMethod -Uri 'https://rabota.atlassian.net/wiki/rest/api/content/35291510?expand=body.storage,version.number' -Headers $headers
$document.version.number = $document.version.number + 1
$document.body.storage.value = 'Hello World from PowerShell'
try {
Invoke-RestMethod -Method Put -Uri 'https://rabota.atlassian.net/wiki/rest/api/content/35291510' -Headers $headers -Body ([System.Text.Encoding]::UTf8.GetBytes(($document | ConvertTo-Json -Depth 100 -Compress)))
Write-Host "Success" -ForegroundColor Green
} catch {
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$body = $reader.ReadToEnd()
Write-Host $body -ForegroundColor Red
}
Note: confluence will fail with error like Content body cannot be converted to new editor format
from http.client import HTTPSConnection
from base64 import b64encode
import json
# Step 0 - preparations, instead of password API token should be used, can be retrieved here: https://id.atlassian.com/manage/api-tokens
con = HTTPSConnection("rabota.atlassian.net")
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization' : 'Basic %s' % b64encode(b"alexandrm@rabota.ua:************").decode("ascii")
}
# Step 1 - retrieve document
con.request('GET', '/wiki/rest/api/content/35520689?expand=body.storage,version.number', headers=headers)
data = json.loads(con.getresponse().read())
print('Current version number:', data['version']['number'])
print('Current body storage value:', data['body']['storage']['value'])
# Step 2 - change it, note that document should be formatted as described here https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html
data['version']['number'] = int(data['version']['number']) + 1
data['body']['storage']['value'] = 'Hello World from Python'
# Step 3 - save it
con.request('PUT', '/wiki/rest/api/content/35520689', json.dumps(data), headers=headers)
res = con.getresponse()
print(res.status, res.reason)