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

1 vote
Accepted answer

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

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
Published Friday in Agile

Are you a Jira Service Desk agent? We want to talk to you!

Are you a whiz at handling tickets and looking at how you can further optimize your workflow with automation? Do you tackle detailed customer support questions while simultaneously getting flooded wi...

63 views 0 3
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you