Attaching a file via RestAPI to a Confluence page draft

I upload a file via atlassian-connect-express to a confluence attachment, from a dynamic content macro.

this.httpClient(userAccountId).post('/rest/api/content/{id}/child/attachment', input);

input is

{
            pathParameters: {
                id: confluencePageId
            },
            formdata: {
                file: [
                    stream,
                    {
                        filename: `File-${processId}`
                    }
                ]
            },
            headers: {
                'X-Atlassian-Token': 'nocheck',
                'Content-Type': 'multipart/form-data'
            }
        }

That works fine. Unless I try this from a macro that has been created in a not yet published draft of a confluence page.

Two questions:

  1. Is it possible to use this endpoint with drafts
  2. If not, how can I determine from a dynamic content macro (custom editor) if the enclosing Confluence page is a draft and has never been published before?

Hi @m.schmidt ,

I haven’t actually tested the following - it is based on my reading of the API.

POST /wiki/rest/api/content/{id}/child/attachment takes a query parameter status which defaults to current if not provided, but can be set to draft when the attachment should be added as part of the current draft. If your dynamic content macro is adding the attachment, then I suspect you always should set the status=draft query parameter so that the attachment occurs as part of the user’s drafting of the page.

By the way, to determine whether the page has not yet been published, you can call AP.context.getContext() and observe the property confluence.content.version. If the version is 1, then the page has not yet been published. Alternatively, there’s a content.version context parameter which you can use so long as the macro is not declare as cacheable: true.

Regards,
Dugald

I have also created https://jira.atlassian.com/browse/CONFCLOUD-71905 since it seems like a gap in our documentation.

2 Likes