Upload attachments to confluence via rest api
Asked by Zhenye Na:

I am able to create / upload an attachment file to our confluence via rest api call scripts below:

curl -L -D- -u username:password -X POST -H "X-Atlassian-Token: no-check;" "file=@\<path to file\>" https://\<hostname\>/rest/api/content/{page_id}/child/attachment

This script works and I found the attachment icon in the confluence page. However the icon is sooo small next to the header and it is hard to find. Since the page I created is for display report to senior engineers and managers who are usually very busy.

Is there are way to make the attachment in the body of the page so that it is easy to find or is there a way to post image / snapshots directly in the body of the page?

Thanks!


Answers:
author: DPKJ
body: [@Zhenye Na](/t5/user/viewprofilepage/user-id/3613863)Welcome to the community!

First of all very nice question. Now, let jump to the possible solutions that can presumably help you resolve your issue,

**Solution 1 - Using HTML Macro and Base64 Encode Image**

* Push image directly into confluence page, using structured format of HTML Macro

```
< ac:structured-macro ac:name = "html" >   < ac:plain-text-body > <![CDATA[<div><img height="" width="" src="data&colon;image/png;base64,...." />"</div>]]> </ ac:plain-text-body ></ ac:structured-macro >
```

* You can find more details about this on this page - <https://stackoverflow.com/questions/30194918/how-do-you-insert-a-confluence-macro-into-a-page-created-by-the-confluence-rest>

**Solution 2 - Displaying attachment from source**

* Take a look at this question here - <https://community.atlassian.com/t5/Confluence-questions/Need-to-know-how-to-insert-attachments-in-Page-body-under-Space/qaq-p/1148469>

comments: author: Zhenye Na
body: Followed the solution from <https://community.atlassian.com/t5/Confluence-questions/How-to-edit-the-page-content-using-rest-api/qaq-p/904345>

got json response via a PUT request

here is the body section of response

```
"body": {
"storage": {
"value": "<p>Display attachment in body of the page</p>",
"representation": "storage",
"_expandable": {
"content": "/rest/api/content/98797437"
}
},
"_expandable": {
"editor": "",
"view": "",
"export_view": "",
"styled_view": "",
"anonymous_export_view": ""
}
},
```

But the pdf attachment does not display in the page yet, am I missing something ?

I ran

```
PUT /rest/api/content/<PAGEID>?expand=body.storage
```

With a Payload of:

```
{
 "type":"page",
 "title":"TITLE OF PAGE", 
 "version":{"number":n},
 "body":{
 "storage":{
 "value":"BODY OF PAGE",
 "representation":"storage"
 }
 }
}
```


author: DPKJ
body: [@Zhenye Na](/t5/user/viewprofilepage/user-id/3613863)I don't think PDF's are displayed inline by default. You can only add link to them.

For PDF Preview, you can try some external plugin from Marketplace, I myself don't use any currently so can't suggest, sorry for that.


author: Zhenye Na
body: Thanks! at least I know this cannot be done currently.


author: Zhenye Na
body: Is it possible to display images in the body using rest api? if so, any instructions?

Thanks!!!


author: Zhenye Na
body: Resolved


author: kiran kV
body: iof


author: Alok Juneja
body: [@DPKJ](/t5/user/viewprofilepage/user-id/690016)

Hi DPK. I would really appreciate if you share a working example of Solution 1, that you mentioned above, we are encountering an issue with it, we get error:

Uncaught SyntaxError: Unexpected identifier at Line 10

Our code is:

var jsondata = {  
"id": "20054524",  
"version": {  
"number": 19  
},  
"title": "EnterNewValue",  
"type": "page",  
"body": {  
"storage": {  
"value":"\<p\>Adding image to page\</p\>\<ac:structured-macro ac:name = "html"\>  
\<ac:plain-text-body\>  
\<!\[CDATA\[\<div\>\<img height="720px" width="1512px" src="data\&colon;image/png;base64,iVB...." /\>\</div\>\]\]\>  
\</ac:plain-text-body\>  
\</ac:structured-macro\>",  
"representation": "storage"  
}  
}  
}

comments: author: DPKJ
body: [@Alok Juneja](/t5/user/viewprofilepage/user-id/3761805) I think only mistake you are making it not escaping html sent in value.


author: Alok Juneja
body: Thanks


author: noorbasha
body: Hi

can you guide me, i am struck to upload a image in confluence with rest api. i don't know how to solve this problem.


Tags: confluence-server

server