confluence pages - get attached files via REST API

Kai Henning January 4, 2016

Hello, in my web application (developed in java) I want to display data from a confluence system (running on different server, in other domain).
To obtain the data, I use the REST API. This works fine for textual data from confluence pages. I wonder how can I get the attached files in binary form.
There is no REST API for this. Has anyone a suggestion, how I can achieve this.
    
This question is related to https://answers.atlassian.com/questions/22650542.

Thanks, Kai

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Answer accepted
Robert Krause
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.
January 4, 2016

Hello Kai,

you can use the REST Endpoint CONFLUENCE_BASE/rest/api/content/{id}/child/attachment to get information about the attachment for a content with a given id. The Information contains the URI to access or download the files.


For a page with the attachments my_attachment.pdf and img_welcome.png, the response will look like:

{
  "results": [
    {
      "id": "att884780",
      "type": "attachment",
      "status": "current",
      "title": "my_attachment.pdf",
      "metadata": {
        "mediaType": "application/pdf",
        "comment": ""
      },
      "extensions": {
        "mediaType": "application/pdf",
        "comment": ""
      },
      "_links": {
        "webui": "/display/ds/Neue+Space+Homepage?preview=%2F98334%2F884780%2Fmy_attachment.pdf",
        "download": "/download/attachments/98334/my_attachment.pdf?version=1&modificationDate=1451988725383&api=v2",
        "self": "http://CONFLUENCE_BASE/rest/api/content/att884780"
      },
      "_expandable": {
        "container": "",
        "children": "/rest/api/content/att884780/child",
        "history": "/rest/api/content/att884780/history",
        "ancestors": "",
        "body": "",
        "version": "",
        "descendants": "/rest/api/content/att884780/descendant",
        "space": "/rest/api/space/ds"
      }
    },
    {
      "id": "att131105",
      "type": "attachment",
      "status": "current",
      "title": "img_welcome.png",
      "metadata": {
        "mediaType": "image/png"
      },
      "extensions": {
        "mediaType": "image/png"
      },
      "_links": {
        "webui": "/display/ds/Neue+Space+Homepage?preview=%2F98334%2F131105%2Fimg_welcome.png",
        "download": "/download/attachments/98334/img_welcome.png?version=1&modificationDate=1325853137007&api=v2",
        "self": "http://CONFLUENCE_BASE/rest/api/content/att131105"
      },
      "_expandable": {
        "container": "",
        "children": "/rest/api/content/att131105/child",
        "history": "/rest/api/content/att131105/history",
        "ancestors": "",
        "body": "",
        "version": "",
        "descendants": "/rest/api/content/att131105/descendant",
        "space": "/rest/api/space/ds"
      }
    }
  ],
  "start": 0,
  "limit": 50,
  "size": 2,
  "_links": {
    "self": "http://CONFLUENCE_BASE/rest/api/content/98334/child/attachment",
    "base": "http://CONFLUENCE_BASE",
    "context": "/"
  }
}


Best Regards,

Robert

Rahul Purohit July 2, 2019

I did the same to get information of all attachments of a page but unfortunately I got 404.

https://<company-domain>/rest/api/content/<page-id>/child/attachment

3 votes
Andreas Ebert
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.
January 4, 2016

You use the Confluence's REST resource GET /rest/api/content/{id}/child/attachment to get a list of attachments. Each entry in the results array represents one attachment. To get to its data, you access the URL under: _links.download

Katarina Godden January 19, 2018

Andreas,

What do you mean by "access the URL under _links.download"?

Thanks

Andreas Ebert January 22, 2018

In the JSON response under "_link" -> "download" there is a URL. Do a GET request on that URL to download the attachment data.

Katarina Godden January 23, 2018

Thank you!

0 votes
Kai Henning January 5, 2016

Hi Robert, Hi Andreas, thanks for your answers. The link or download path I had already been determined. I forgot until now, however, to put in URLConnection object the "Authorization" header. Now I have access to the data via URLConnection.getInputStream().


Thanks

TAGS
AUG Leaders

Atlassian Community Events