It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Best way to get subpart only of page content with Confluence API?

Hi,
Using Confluence API (not plugins), I want to get subpart only of page content (not entire page) in the aim to update it later.
I tried with page properties but I can’t get only unique property but a list of all pages with properties with next command (/rest/masterdetail/1.0/detailssummary/lines?cql=type=page&spaceKey=XXX&headings=XXX).
What is the best way to put data in the page structure and find easily with Confluence API?

Thank in advance for your answer.

1 answer

Use the python module from here 

https://pypi.org/project/atlassian-python-api/

It's easy to use. It has useful examples too. 

Hi, thanks for the suggestions. Would you please provide an example of patching a specific part of the page, e.g. adding an admonition to the page? Thank you.

Thanks for the explanation.

Currently I am on PHP but I will look for Python because the API actually has 2 interesting methods (get_page_property and set_page_property)...

 

You would have to import packages and add authentication as shown in above docs. 

Okay. Here is the example of getting the page content and replacing the content. I am removing {group3} references in the page to nothing. 

def rest_page_update(space,page_id,title):
content = confluence.get_page_by_id(page_id,expand='body.storage').get('body').get('storage').get('value')
body = re.sub('{group3}','', content, flags = re.M)
confluence.update_page(None, page_id, title, body, type='page')
Like Stan Ry likes this

Thank you, that's what I wanted to do.
I will test on my side if everything works as I want in Python.
Regards,

@jira_guy 

Fantastic! Thanks a bunch. Could you please post a working sample that could be used to get started. 

I mean a working sample that shows how to auth on confluence and call your 

rest_page_update

API. Kinda new to Python, so any short sample that would work from scratch is highly appreciated.

Thank you. That replacement sample is really cool: so simple and demonstrative.

here you go...

import fileinput
import re
import os
import hashlib
from collections import defaultdict
import shutil
from atlassian import Confluence
confluence = Confluence(
url='https://ursite.url.com',
username='username',
password='*****')
def rest_page_update(space,page_id,title):
content = confluence.get_page_by_id(page_id,expand='body.storage').get('body').get('storage').get('value')
body = re.sub('{group3}','', content, flags = re.M)
confluence.update_page(None, page_id, title, body, type='page')
Like Stan Ry likes this

Whoaa, thank you @jira_guy That's what I am hugely missing from Atlassian's docs. Samples. Any clue on how could I skip the SSL check?

I get the following error:

Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]

When invoking `requests` component, I used to employ the following line to skip SSL verification:

requests.packages.urllib3.disable_warnings()

How is it done with `atlassian` component?

It feels like it's done like that

confluence = Confluence(
url=WIKI_WEB,
username=userName,
password=userPassword,
verify_ssl=False)

and like if setting `verify_ssl` to False is enought. But. Then I get:

TypeError: 'NoneType' object is not subscriptable

 when processing this line:

confluence.update_page(None, page_id, title, body, type='page')

Any clues?

Thank you!

Hi,
I tried with Python Confluence API:
confluence = self.get_confluence()
status = confluence.page_exists(space=self.page_space, title=self.page_title)
if status:
self.page_id = confluence.get_page_id(self.page_space, self.page_title)
print(confluence.get_page_properties(self.page_id))
Problem is same as Web Service: Existing page properties from Confluence Web site are not visible by API...
But when I create a property by API:
property_data1 = {"key": "1234",
"value": {"Test API ": "en cours"}, "version": {"number": 1, "minorEdit": 'false'}}
prop = confluence.set_page_property(self.page_id, data=property_data)
It work fine in database, result from get_page_properties is:
"results": [
{
"id": "2955040",
"key": "1234",
"value": "Test API ",
"version": {
"when": "2019-09-09T17:19:18.533+02:00",
"message": "",
"number": 1,
"minorEdit": false,
"hidden": false
},
"_links": {
"self": "https://confluence.test/rest/api/content/2954600/property/1234"
},
"_expandable": {
"content": "/rest/api/content/2954600"
}
},
{
"id": "2955032",
"key": "example-Property001",
"value": [
"Test API ",
"en cours"
],
"version": {
"when": "2019-09-09T17:01:01.192+02:00",
"message": "",
"number": 1,
"minorEdit": false,
"hidden": false
},
"_links": {
"self": "https://confluence.test/rest/api/content/2954600/property/example-Property001"
},
"_expandable": {
"content": "/rest/api/content/2954600"
}
}
],
"start": 0,
"limit": 10,
"size": 2,
"_links": {
"self": "https://confluence.test/rest/api/content/2954600/property",
"base": "https://confluence.test",
"context": ""
}
}
Unfortunately, API page properties are not visible on Confluence Web site…
I tried changing profile as admin for the same result…
Do you have some idea of what happen ?
Thank in advance for your answer.
Regards,

Like Stan Ry likes this

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

How is your team having fun and bonding, remotely, utilizing Confluence?

Thanks everyone for answering last week’s question. The winner of the random drawing from those who commented is: @LarryBrock I’ll contact you separately with your prize details. This wee...

327 views 9 7
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you