Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Confluence API attachment upload

Felix Köhn October 28, 2021

Hey all,

I'm trying to load an attachment to a Confluence Space.

I'm using this:

 

USER_NAME=XXXXX
USER_PASSWORD=XXXXXX
CONFLUENCE_BASE_URL=http://<CONFL_URL>
ATTACHMENT_TYPE=file
ATTACHMENT_FILE_NAME=myfile2.txt
PAGE_ID=<PAGEID>

curl -u $USER_NAME:$USER_PASSWORD \
-X POST \
-H "X-Atlassian-Token: nocheck" -F "file=@${ATTACHMENT_FILE_NAME}" -F "comment=File attached via REST API" \
${CONFLUENCE_BASE_URL}/rest/api/content/${PAGE_ID}/child/attachment 2>/dev/null \
| jq -r '.results[].title'

REST_CONTENT_FULL_OUTPUT=$(curl -u ${USER_NAME}:${USER_PASSWORD} \
${CONFLUENCE_BASE_URL}'/rest/api/content/'${PAGE_ID}'?expand=body.storage,version,space' 2>/dev/null)

 

if [ ${ATTACHMENT_TYPE} = "image" ]; then
echo ${REST_CONTENT_FULL_OUTPUT} | \
jq '{body: {storage: {value: (.body.storage.value + "<p><ac:image ac:height=\"250\"><ri:attachment ri:filename=\"'${ATTACHMENT_FILE_NAME}'\" /></ac:image></p>"), representation: .body.storage.representation}}, id: .id, type: .type, title: .title, space: {key: .space.key}, version: {number: (.version.number + 1)}}' \
> modified-page-data.json
else
echo ${REST_CONTENT_FULL_OUTPUT} | \
jq '{body: {storage: {value: (.body.storage.value + "<ac:structured-macro ac:name=\"view-file\" ac:schema-version=\"1\" ac:macro-id=\"'${MACRO_UUID}'\"><ac:parameter ac:name=\"name\"><ri:attachment ri:filename=\"'${ATTACHMENT_FILE_NAME}'\" /></ac:parameter><ac:parameter ac:name=\"height\">250</ac:parameter></ac:structured-macro>"), representation: .body.storage.representation}}, id: .id, type: .type, title: .title, space: {key: .space.key}, version: {number: (.version.number + 1)}}' \
> modified-page-data.json
fi

curl -u ${USER_NAME}:${USER_PASSWORD} \
-X PUT \
-H 'Content-Type: application/json' \
--data @modified-page-data.json \
${CONFLUENCE_BASE_URL}'/rest/api/content/'${PAGE_ID} 2>/dev/null \
| jq -r '.id'

Everything works fine, as long as the Confluence Page <PAGEID> does not have a attachment called "myfile2.txt".

 

As soon as I upload the file and rerun the Script, I'd expect I would upload the file again as the newest version. The other myfile2.txt file would be the older version. 

Instead, nothing happens and I get the error:

jq: error (at <stdin>:0): Cannot iterate over null (null)

 

Could anybody please help with that problem?

Thank you in advance!

 

Kind regards,

Felix

1 answer

0 votes
Thomas Deiler
Community Champion
October 30, 2021

Dear @Felix Köhn ,

yes, you can! ;)

a) get the ID of your attachment to update:

GET /rest/content/{id}/child/attachment

b) update the attachment data:

POST /rest/content/{id}/child/attachment/{attachmentId}/data

So long

Thomas D

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events