How to post attachments to confluence page
Asked by Etienne:

Hi there, I have searched but with no luck so far? Is it possible to post .png images to a confluence page using either Rest API or Java API?The .png images are sourced from a 3rd party tool and the idea is to build a plugin that will export and import (post) the .png images to Confluence pages?

Any help will will be most welcome?


Answers:
author: rsperafico
body: Hello Etienne,

Thank you for your inquire.

You could be running the following in Python, for instance:

```
# ===============# upload attachment# ===============import requestsimport json# [CONFLUENCE-BASE-URL], i.e.: http://localhost:8090# [CONTENT-ID], get content ID by running '[CONFLUENCE-BASE-URL]/rest/api/content'url = '[CONFLUENCE-BASE-URL]/rest/api/content/[CONTENT-ID]/child/attachment'headers = {"X-Atlassian-Token": "nocheck"}data = {"comment":"this is my file"}# please, uncomment to attach inline content#files = {'file': ('report.xml', '&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;note&gt;&lt;to&gt;RECIPIENT&lt;/to&gt;&lt;from&gt;SENDER&lt;/from&gt;&lt;heading&gt;ATTACHMENT&lt;/heading&gt;&lt;body&gt;CONTENT&lt;/body&gt;&lt;/note&gt;')}# please uncomment to attach external file#files = {'file': open('text.txt', 'rb')}#files = {'file': open('image.png', 'rb')}# upload file to page# [USERNAME], i.e.: admin# [PASSWORD], i.e.: adminr = requests.post(url, data=data, auth=('[USERNAME]', '[PASSWORD]'), files=files, headers=headers)print(r.status_code)print(r.text)
```

Alternatively, you could be running the following cURL command:

```
# [USERNAME], i.e.: admininstrator# [DOMAIN], i.e.: mycompany# [CONTENT-ID], i.e.: 9666562curl -D- -u [USERNAME] -p -H "X-Atlassian-Token: nocheck" -X POST -F file=@/full/path/to/image.png https://[DOMAIN].atlassian.net/wiki/rest/api/content/[CONTENT-ID]/child/attachment
```

If you find this answer useful, I would kindly ask you to accept it so the same will be visible to others who might be facing the same issue you have inquired.

Thank you for your understanding.

---

Kind regards,  
Rafael P. Sperafico  
Atlassian Support

comments: author: Dan McMahill
body: Thanks for the very useful python example. I'm curious though, it seems to work for me when I first upload but if my change the file I want to attach and then run the python program again, I get a 400 Bad Request.

Is there some magic needed to allow updating an attachment?


author: Kalyan
body: I am using same CURL command to upload multiple html files but its not allowing me to do so. Could you please suggest. I am using Confluence cloud.

```
curl -D- -u $USERNAME:$PASSWORD -X PUT -H "X-Atlassian-Token: nocheck" -F "file=@code/pydoc/*.html" -F "minorEdit=false" 'https://alm-tuigroup.atlassian.net/wiki/rest/api/content/504955238/child/attachment'?
```


author: antony terrence
body: [@rsperafico](/t5/user/viewprofilepage/user-id/781554) , is there a way to include encrypted username and password in the request?


author: Ming
body: Hi Rafael ,

Thanks so much for your great python example, it's very helpful for me! great!

BR//Ming


author: Larry Talley
body: I think the answer to Dan McMahill's question above is that once an attachment is attached, if you want to update that attachment, then you have to post to a different URL: you need to post to the attachment's data resource. In my case the resource for the attachment update was:

.../rest/api/content/253493424/child/attachment/281051237/data

I was able to find the resource for the attachment via:

curl -k https://.../rest/api/content/253493424/child/attachment \| python -mjson.tool


author: Bob Swift (personal)
body: Using the [Confluence Command Line Interface (CLI)](https://bobswift.atlassian.net/wiki/display/CSOAP) might make it easier - use the [addAttachment](https://bobswift.atlassian.net/wiki/display/CSOAP/Reference#Reference-addAttachment) action.


author: antony terrence
body: You can do a multipart POST call to upload multiple attachments at once. I wrote something like the following with apache http client mime library.

```
public int uploadAttachments(String path, List<String> attachmentNames) throws Exception {
CloseableHttpClient client = getClosableHttpClient();
HttpPost request = new HttpPost(buildURI(path));
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

for (String s : attachmentNames) {
if ("binary".equals(Utils.getFileType(s))) {
// System.out.println(s);
// builder.addBinaryBody("file", null)
builder.addBinaryBody("file", new File(s), ContentType.DEFAULT_BINARY, new File(s).getName());
} else if ("text".equals(Utils.getFileType(s))) {
builder.addBinaryBody("file", new File(s), ContentType.TEXT_PLAIN, new File(s).getName());
} else {

}
}
HttpEntity entity = builder.build();
request.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + this.creds);
request.setHeader("X-Atlassian-Token", "no-check");
request.setEntity(entity);
// System.out.println(EntityUtils.toString(request.getEntity()));
CloseableHttpResponse response = client.execute(request);
// System.out.println(EntityUtils.toString(response.getEntity()));
return response.getStatusLine().getStatusCode();
}
```


author: KOSHTI NIKHILESH
body: The python code will move the file in the attachment section. How can we show it in the page content. Any help?


author: Saquib Fraz
body: Thanks for the great Python program. Was breaking my head from morning . Finally this beautiful piece of code.

Thanka a lot Rafael P. Sperafico  


author: Supreetha Upadhya
body: Moving attachments from its current space to a different space:

Using curl, the following can be used to attach the file to a page in a different space:

```
curl -v -u 'admin:XXX? -X PUT -d '{"id":"attXXX?,?type":"attachment","title?:?xxx.jpeg","status":"current","ancestors":[],"version": {"number":"2"},"container": {"id": "34514","type": "page","status": "current","title": "Andro Homepage!","space": {"id": 34513,"key": ?AR?,?name": "Andro"}}}' -H 'X-Atlassian-Token:<access-token>' -H 'Content-Type:application/json' https://XXX.atlassian.net/wiki/rest/api/content/<parent page id>/child/attachment/attXXX | python -m json.tool
```


Tags: attachment

cloud

confluence-cloud

rest

upload