'Upload Confluence page content containing {code} macros through API

What I'm trying to achieve is to upload a Confluence page content that contains code examples, and I'd like these code examples to use the {code} macro plugin that provides syntax highlighting when viewing the page.

I've found that the code macro stores 2 formats on Confluence, respectively for body.storage and body.view:

<ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro- id="37fecf11-d435-452a-90c7-da19f3821b4c">
  <ac:parameter ac:name="language">bash</ac:parameter>
  <ac:parameter ac:name="linenumbers">true</ac:parameter>
  <ac:plain-text-body><![CDATA[ // code goes here ]]></ac:plain-text-body>
</ac:structured-macro>

and

<div class="code panel pdl" style="border-width: 1px;">
   <div class="codeContent panelContent pdl">
     <pre class="syntaxhighlighter-pre">
     // code goes here
     </pre>
   </div>
</div>

I've tried uploading both to Confluence using the API, but each time, the code block is rendered as a simple <pre/> element, and syntax highlighting is not rendered.

Any help appreciated.

Note: This is how I update the content through the API: https://docs.atlassian.com/atlassian-confluence/REST/latest-server/#content-update Typically:

HTML = '<div class="code panel pdl" style="border-width: 1px;">
   <div class="codeContent panelContent pdl">
     <pre class="syntaxhighlighter-pre">
     $ DIST=`cat /etc/*release`
     $ echo $DIST
     </pre>
   </div>
</div>'

data = json.dumps(
    {
        'id': '%d' % PAGEID,
        'type': 'page',
        'title': TITLE,
        'space': {
            'key': SPACE
        },
        'version': {
            'number': VERSION +1
        },
        'body': {
            'storage': {
                'representation': 'storage',
                'value': HTML
            }
        }
    }   
)
rPut = requests.put(
    url,
    data = data,
    auth = (USER, PWD),
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
)

Any help appreciated,



Solution 1:[1]

when I get content in storage format, a syntax-highlighted code block, looks like the below

<div class="code panel pdl conf-macro output-block" data-hasbody="true" data-macro-name="code" style="border-width: 1px;">
    <div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;">
        <b>SANITIZED CODE HEADER</b>
    </div>
    <div class="codeContent panelContent pdl">
        <pre class="syntaxhighlighter-pre" data-syntaxhighlighter-params="brush: bash; gutter: false; first-line: 1; theme: DJango" data-theme="DJango">
        SANITIZED CODE CONTENT
        </pre>
    </div>
</div>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pavel Kovtunenko