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

How to create a page with a table using REST API?

I currently have the task of importing a large body of text with many notes and ids and descriptions and would like to represent it in a more readable format of by placing all of these into a series of tables.

Reading the text, extracting information, making connection between data in the text was straightforward enough, as was converting this information into an HTML table.

The problem lies with the actual creation of a page with this table in it, as I cannot send a POST request to create a new page with the raw HTML table as content

$data = array("type" => "page", "title" => "testings".time(), "space" => array("key" => "$uploadSpace"), "body" => array("storage" => array("value" => "$htmlTable", "representation" => "storage")));

because the html wouldnt be properly 'escaped' in PHP I would do this with htmlspecialchars() but sending this up simply shoots up my text with the tags in view and no actual table.

One solution I have found for this would be to contain the entire table in a html macro. This allows viewing of the table, but makes it difficult to edit in the WYSIWYG editor for the people using the wiki, which would make this entire exercise fruitless and I wouldnt like to rely on the macros as much.

$data = array("type" => "page", "title" => "testings".time(), "space" => array("key" => "$uploadSpace"), "body" => array("storage" => array("value" => "<ac:structured-macro ac:name=\"html\"><ac:plain-text-body><![CDATA[$htmlTable]]></ac:plain-text-body></ac:structured-macro>", "representation" => "storage")));

Would appreciate any help thank you.

 

1 answer

I'm not sure what language you were attempting this with, but here's a simplified python example:

 

import datetime
import requests
# Just creating a simple table that looks like thus:
# ||One||Two||
# |OneA|OneB|
pagecontent = '<p class="auto-cursor-target"><br /></p><table><colgroup><col /><col /></colgroup><tbody><tr><th>One</th><th>Two</th></tr><tr><td>OneA</td><td>OneB</td></tr></tbody></table><p><br /></p>'
reportobj = {}
reportobj["type"] = "page"
reportobj["title"] = "Your Page Title"
reportobj["space"] = {"key": "YOURSPACEKEY"}
reportobj["body"] = {"storage": {"value": "%s" % pagecontent, "representation": "storage"}}
url = 'https://your.wiki.address/confluence/rest/api/content/'
headers = {'Content-Type': 'application/json'}
wikiuser = "Your User Name here"
wikipasswd = "Your Password"
response = requests.post(url, data=json.dumps(reportobj), headers=headers, auth=(wikiuser, wikipasswd))
# response.status_code should equal 200

Works beautifully, thanks!!!

Suggest an answer

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

Lessons and Learnings: Six Months of Working Remote [Discussion]

Hey there, folks! For most of us, the past six months- yes, you read that right- have been a journey. More people than ever before have pivoted to working remotely, and navigating being on-scre...

8,411 views 6 6
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