Adding an attachment to an existing Page

Hi there,

I’ve been able to add a new page to Confluence (with Postman) by using the base-url/rest/api/content and doing a POST with a proper JSON body. This works. It also returns an ID that use in the next api-calls.

Next action I would like to do is to add an attachment to this new page. For this I do a POST on the base-url/rest/api/content/{{PageID}}/child/attachment?status=current. In the body I choose form-data and specify a file. This also works. When I look at the page in Confluence I see there is an attachment. However it is not yet part of this page.

Following the documentation I should do a PUT on the base-url/rest/api/content/{{PageID}}/child/attachment/ but I am lost on what is supposed to be in the body… Different documentation state different approaches but neither of them work for me.

I use something like this in the raw json body:

{
“version”: {
“number”: “2”
},
“id”: {{AttachmentID}},
“type”: “attachment”,
“title”: “Plaatje”,
“metadata”: {
“mediaType”: “image/jpeg”,
“comment”: “geen commentaar”
},
“container”: {
“id”: {{PageID}},
“type”: “page”
}
}

Blockquote

When I do this I get a 405 (Method not allowed)
Any help would be appreciated…

Thanks John

1 Like

Hi @JohnvanKaam ,

This endpoint doesn’t take JSON in the request body, it relies on form data with the content type multipart/form-data.

Postman supports this, but it’s very hidden. You have to name one of the form data fields file, then click a hidden dropdown on the right to change the type from Text to File. For your value, select the file you want to upload.

Also remember to set the header X-Atlassian-Token: nocheck

In code, you should check how your request library does file uploads with multipart form data.

Regards,
Michael

1 Like

Hi Michael, thanks for helping out. Are you sure you’re not telling me how to upload an attachment? Instead of how I can attach the attachment to the targetpage?

As I understand; after having added a new page, you can use the ID from the response to upload an attachment, and with the return from this (POST) you should be able to re-use the ID and PUT the attachment on the target page.

Now it would be nice if you could upload an attachment and in the same API attach it to the targetpage but I don’t think that is possible.

For reference, I looked over https://confluence.atlassian.com/confkb/using-the-confluence-rest-api-to-upload-an-attachment-to-one-or-more-pages-1014274390.html and also https://www.postman.com/api-evangelist/workspace/atlassian-confluence/request/35240-bc038e16-397d-4388-8dcb-55d6fb53699e

Thanks again…

Ah, apologies, you want to associate an existing attachment with an existing page, correct?

Can you please try the Update Attachment Properties API: https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-content---attachments/#api-wiki-rest-api-content-id-child-attachment-attachmentid-put and specify either the ancestors or container property in your body? Please let me know whether that helps.

Thanks,
Michael

1 Like

Hi Michael, thanks for helping out. I already looked at that documentation. It’s a bit similar to the one I mentioned. I guess I was looking for a more simple example… both of them show a body that has the complete list of options you could supply. Like as in >2000 lines of json… where as I would only like to refer to the attachmentID and the target PageID… (that are already in the URL as well)