It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

confluence pages - get attached files via REST API

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

3 votes
Answer accepted

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

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

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

Andreas,

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

Thanks

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

We want to hear from you! How do you use Slack and Atlassian together?

Hi Everyone! My name is Mina and I am on Atlassian’s Ecosystems Marketing team. Our team is focused on our technology partnerships and marketplace apps. One of Atlassian’s partners is Slack, who ...

73 views 0 4
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you