Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,367,481
Community Members
 
Community Events
168
Community Groups

How to post complex HTML with the rest API

Deleted user Oct 06, 2021

So from what I can tell, there is an object model that can decode simple HTML that’s posted via the rest api, but once I get fairly complicated with java script and other junk the model breaks down and won’t accept the post.

However, it is clear that the complicated HTML can be presented because it works inside of the HTML macro – if I add that manually and cut and paste the complicated HTML, it displays adequately (I don't need the javascript to work)

I have 100000 pages to load, so I can't actually do this manually.

From searching the web it looks possible, but detailed and finicky, to post the HTML Macro via the rest API. Can someone describe the payload for the "create page" post that would be needed to do this?

I've got a python script that can post the simple html using a structure like:
{
"type":"page",
"title":"simplejsonteest",
"space":
{"key":"CSE"},
"body":
{"storage":
{"value":
<content_from_html_file>,
"representation":"storage"}}}

how can I wrap or modify content_from_html_file to have it end up inside the HTML macro?

1 answer

1 accepted

0 votes
Answer accepted
Deleted user Oct 07, 2021

Actually I found that on the right hand navigation there is "View Storage Format" which allows me to see the macro XHTML: 

<ac:structured-macro ac:name="html" ac:schema-version="1" ac:macro-id="150f8fa2-7ad1-46c4-a1b4-6b394d59cfbf"><ac:plain-text-body><![CDATA[<b>My data!</b>]]></ac:plain-text-body></ac:structured-macro>

I'm not sure if that ID is static across instances, you might want to check your own confluence.

Using the examples on this page https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/ to create the json, I can post this macro and any arbitrary HTML page to my project.


In my case I used python instead of curl and inserted the html page into the macro in the cdata section and then uploaded that. Because my page had lots of quotes and other characters, using curl was problematic. Python sorted out the jsonification of my input data for free.

There's probably a more pythonic way to do it, but this works to create the <value> needed in 
"body":
{"storage":
{"value": <value>

import fileinput

def wrap_page(filename)

outfile = "{}.wrapped".format(filename)
filenames = [ "html_macro_open.xhtml", filename, "html_macro_close.xhtml"]
with open(outfile, 'w') as fout, fileinput.input(filenames) as fin:
for line in fin:
fout.write(line)

with open(filename, 'r') as file_handle:
return file_handle.read()

Could you please post your python request lines ?  and which version of confluence are you using?

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS

Atlassian Community Events