Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to insert an attachment into a Confluence page using Python

Bhavani Balakrishnan September 28, 2016

Hi,

Iam trying to use the attachment macro to pull all the files that i have attached into the main page. But always getting an error.

 My code is

headers1 = {'Content-Type': 'application/json'}
data1 = {"id":id,"type":"page","title":file,"space":{"key":"~CCala700000"},"body": {"storage": {"value": "<ac:structured-macro ac:name=\"attachments\"><ac:parameter ac:name=\"old\">false<\/ac:parameter> <ac:parameter ac:name=\"patterns\">.*xlsx<\/ac:parameter> <ac:parameter ac:name=\"sortBy\">name<\/ac:parameter> <ac:parameter ac:name=\"sortOrder\">ascending<\/ac:parameter><ac:parameter ac:name=\"labels\"><\/ac:parameter> <ac:parameter ac:name=\"upload\">false<\/ac:parameter> <\/ac:structured-macro>","representation":"storage"}},"container":{"id":id}}
r = requests.post('https://confluence.XXXX.com/rest/api/contentbody/convert/view/',data=json.dumps(data1),headers=headers1,auth=(username, password))
print(r.status_code)
print(r.text)

The error is 500 with the below message

{"statusCode":500,"data":"authorized":false,"valid":true,"errors":],"successful":false,"notSuccessful":true},"message":"com.atlassian.confluence.api.service.exceptions.ServiceException: java.lang.UnsupportedOperationException: Cannot convert from null to view"}

Even i post tried posting like the below but not successful. Iam trying this for the first time. Any help is much appreciated.

r = requests.post('https://confluence.XXXX.com/rest/api/content/',data=json.dumps(data1),headers=headers1,auth=(username, password))
print(r.status_code)

I get the below error. Do not know which one is correct

{"statusCode":400,"data":"authorized":false,"valid":true,"errors":],"successful":false,"notSuccessful":true},"message":"Error parsing xhtml: Unexpected character '\\' (code 92) in content after '<' (malformed start element?).\n at [row,col {unknown-source}]: [1,506]"}


Thanks.

Bhavani

3 answers

0 votes
Bhavani Balakrishnan September 29, 2016

At last i got the response as 200 with the below response but the attachments are not added to the page. Do not know the root cause of the issue

200
{"value":"\n\n \n\n<div class=\"plugin_attachments_container conf-macro outpu
t-block\" data-hasbody=\"false\" data-macro-name=\"attachments\">\n <div clas
s=\"plugin_attachments_table_container\">\n <fieldset class=\"hidden\">\n
<input type=\"hidden\" class=\"plugin_attachments_macro_render_param
\" name=\"pageId\" value=\"46047404\" />\n <input type=\"hidden\" nam
e=\"deleteConfirmMessage\" value=\"Are you sure you want to send the attached fi
le \" to=\"\" the=\"\" only=\"\" a=\"\" space=\"\" administrator=\"\" can=\"\" u
ndo=\"\" this=\"\" />\n <input t
ype=\"hidden\" class=\"plugin_attachments_macro_render_param\" name=\"upload\" v
alue=\"false\" />\n
<input type=\"hidden\" class=\"plugin_attachments_macro_render_param\" name=
\"old\" value=\"false\" />\n
<input type=\"hidden\" class=\"plugin_attachments_macro_render_para
m\" name=\"patterns\" value=\".*xlsx\" />\n
<input type=\"hidden\" class=\"plugin_attachments_ma
cro_render_param\" name=\"sortOrder\" value=\"ascending\" />\n
<input type=\"hidden\" class=\"pl
ugin_attachments_macro_render_param\" name=\"sortBy\" value=\"name\" />\n
<input type=\"hidde
n\" name=\"outputType\" value=\"display\" />\n </fieldset>\n
\n\n\n\n\n\n\n \n\n\n\n<table class=\"attachments aui\">\n <thead>\n
<tr>\n <th class=\"expand-column attachment-summary-toggle\"> </th>\n
<th class=\"filename-column\"> <span class=\"propersortedby a
scending\"><a href=\"/display/~CCala7000000/ChOptiHeader?sortBy=name&amp;sortOrd
er=descending\">File</a></span>\n </th>\n <th class=\"modified-col
umn\"> <a href=\"/display/~CCala7000000/ChOptiHeader?sortBy=date&amp;
sortOrder=ascending\">Modified</a>\n </th>\n </tr>\n </thead>\n\n
<tbody>\n \n \n <tr i
d=\"attachment-46049726\" class=\"attachment-row\" data-attachment-id=\"46049726
\" data-attachment-idx=\"1\">\n\n <td class=\"attachment-summary-
toggle\">\n <span class=\"icon icon-section-closed\" title=\"
Show more info\"> </span>\n </td>\n\n <td class=\"
filename-column\">\n \n\n \n\n\n<span class=\"icon icon-fi
le-excel\" title=\"Microsoft Excel Spreadsheet\">Microsoft Excel Spreadsheet</sp
an> <a class=\"filename\" href=\"/download/attachments/460474
04/ChOptiHeader.xlsx?api=v2\" title=\"Download\" data-filename=\"ChOptiHeader.xl
sx\" data-type=\"application/vnd.openxmlformats-officedocument.spreadsheetml.she
et\" data-linked-resource-container-id=\"46047404\">\n Ch
OptiHeader.xlsx\n </a>\n <
/td>\n <td class=\"attachment-created modified-column\">\n
<span>26 minutes ago</span>\n <span>by</span>\n
<a href=\" /display/~CCala7000000&#xa;\" class=\"url f
n confluence-userlink\" data-username=\"CCala7000000\">Bhavani Balakrishnan</a>
</td>\n </tr>\n <tr class=\"attachment-summ
ary attachment-summary-46049726 hidden\" data-attachment-id=\"46049726\" data-at
tachment-filename=\"ChOptiHeader.xlsx\">\n\n <td class=\"attachme
nt-summary-toggle\"> </td>\n <td class=\"attachment-details-wrapp
er\" colspan=\"2\">\n\n \n
<p class=\"attachment-labels\">Labels</p>\n \n<div class
=\"labels-section-content content-column\" entityid=\"46049726\" entitytype=\"at
tachment\">\n\t<div class=\"labels-content\">\n\t\t\n <ul class=\"label-list
has-pen\">\n <li class=\"no-labels-message\">\n No labels
\n </li>\n <li class=\"labels-edit-container\">\n
<a href=\"#\" class=\"show-labels-editor\" title=\"Edit Labels\">\n
<span class=\"aui-icon aui-icon-small aui-iconfont-edit-small\">Edit Label
s</span>\n </a>\n </li>\n </ul>\n\n </div>\n</div>\n
\n \n <div class=\
"attachment-buttons\">\n
<a class=\"aui-button previewAttachmentLink\" data-filename=\"ChOptiHeader.x
lsx\" data-file-src=\"/download/attachments/46047404/ChOptiHeader.xlsx?api=v2\"
data-linked-resource-default-alias=\"ChOptiHeader.xlsx\" data-mime-type=\"applic
ation/vnd.openxmlformats-officedocument.spreadsheetml.sheet\" data-linked-resour
ce-container-id=\"46047404\" data-linked-resource-id=\"46049726\">Preview</a>\n
\n
\n <a class=\"aui-button \" href=\"/page
s/worddav/preview.action?fileName=ChOptiHeader.xlsx&amp;pageId=46047404\">View</
a>\n \n
<a class=\"aui-button office-editable\" href=\"/plugins/servle
t/confluence/editinword/46047404/attachments/ChOptiHeader.xlsx\">Edit in Office<
/a>\n
\n <a class=\"aui-button editAtt
achmentLink\" href=\"/pages/editattachment.action?pageId=46047404&amp;fileName=C
hOptiHeader.xlsx&amp;isFromPageView=true\">Properties</a>\n
\n <a clas
s=\"aui-button removeAttachmentLink\" href=\"/pages/confirmattachmentremoval.act
ion?pageId=46047404&amp;fileName=ChOptiHeader.xlsx\">Delete</a>\n
</div>\n
</td>\n </tr>\n </tbody>\n</table>\n </div
>\n <div class=\"plugin_attachments_upload_container\">\n </div>\n
\n <div>\n </div>\n</div>\n","representation":"view","_links":{"ba
se":"https://confluence.XXXX.com","context":""},"_expandable":{"webresource":"",
"content":"/rest/api/content/46047404"}}
-----------------------------------------

 

0 votes
Bhavani Balakrishnan September 28, 2016

Yes, I generate 58 files + additional new files every week and get copied to the confluence every week. I do not want to manually do for all those files.

 

 

0 votes
Rodney Hughes
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 28, 2016

Why are you doing all that??

Just open the page in Edit mode, select Insert>Other macros>Attachments and Save

Is there something special you are trying to achieve.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events