Updating existing Wiki page on Confluence using Python 3. See this answer to know how to create a page using Python.
Make sure that ID is correct of the page you are trying to update. And if you are going to change the title, make sure that the same title does not appear on another page in the same space.
import requests
import json
from requests.auth import HTTPBasicAuth
# set auth token and get the basic auth code
auth_token = "{TOKEN}"
basic_auth = HTTPBasicAuth('{email you use to log in}', auth_token)
# Set the title and content of the page to create
page_title = 'My Existing Page'
page_html = '<p>This is the text that needs to be updated</p>'
page_id = {Page ID}
space_key = '{SPACE KEY}'
# get the confluence home page url for your organization {confluence_home_page}
url = '{confluence_home_page}/rest/api/content/{Page ID}'
# Request Headers
headers = {
'Content-Type': 'application/json;charset=iso-8859-1',
}
# Request body
data = {
'id': {Page ID}
'type': 'page',
'title': page_title,
'space': {'key':space_key},
'body': {
'storage':{
'value': page_html,
'representation':'storage',
}
},
'version': {
'number': 2,
'when': '2017-10-06T15:16:17.501-04:00'}
}
# the version number should be the immediate next version number of the existing document.
# We're ready to call the api
try:
r = requests.put(url=url, data=json.dumps(data), headers=headers, auth=basic_auth)
# Consider any status other than 2xx an error
if not r.status_code // 100 == 2:
print("Error: Unexpected response {}".format(r))
else:
print('Page Updated!')
except requests.exceptions.RequestException as e:
# A serious problem happened, like an SSLError or InvalidURL
print("Error: {}".format(e))
confluence
sandwiched betweenhttps:\wiki.mydomain.com
and/rest/api/content?title=
? – Sandi