Find the total number of attachments in a page using REST API

InfoSec
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 27, 2021

Using the REST API - /api/content/{id}/child/attachment, how do I find the total number of attachments? 

I don't see a totalSize in the json response. Irrespective of the limit parameter value, the size of the result set is 200.

Using the UI, I can visually see the number Attachments (650) when looking at the page properties. 

Is there a way to get this number using REST API? 

 

3 answers

2 votes
tshenolo
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 28, 2023


The attachment REST API call does not return the total number of attachments. 
You have to do something called API pagination and add up all sizes to get the total size.

For reference: REST API - /api/content/{id}/child/attachment?start=0&limit=200

Also the maximum limit you can set for the attachment API call is 200. ie limit=200.

The trick is to increase the start parameter by 200 and check if the json returned by the API call has  _links->next .

For example you can do the following API calls:

YOUR_SERVER/api/content/{id}/child/attachment?start=0&limit=200
YOUR_SERVER/api/content/{id}/child/attachment?start=200&limit=200
YOUR_SERVER/api/content/{id}/child/attachment?start=400&limit=200
YOUR_SERVER/api/content/{id}/child/attachment?start=600&limit=200

until the json returned does not contain _links->next

Add up all the sizes for each call to get the total number of attachments.


Please check the following sample code:  
https://github.com/tshenolo/confluence-restapi-functions

Don't forget to give the repo a star if you find the sample code useful.

0 votes
Roman Wolf
Contributor
October 9, 2024

UPDATE for Confluence REST API v2:

This is not a bug but a feature. The max amount of attachments returned in a single response is limited to 250 and pagination must be used (as mentioned by tshenolo already). 

On Confluence REST API v2 the request/response behaviour differs from v1.

If a page contains more attachments the response will contain a "Link" parameter in the response header.

The same can be found in the response payload body, under "_links"/"next" key:

"results": [
...
],
"_links"
: {
"next": "/wiki/api/v2/pages/<page-id>/attachments?cursor=<token>",
"base": "https://yourdomain.atlassian.net/wiki"
}
}

Use the link value to query to next 250 attachments. Continue as long as the response contains a "Link" header parameter or "_links"/"next" key in the JSON body.

 

This is well documented here: https://developer.atlassian.com/cloud/confluence/rest/v2/api-group-attachment/#api-pages-id-attachments-get

... The number of results is limited by the limit parameter and additional results (if available) will be available through the next URL present in the Link response header.

 

Regards, Roman

0 votes
Thomas Deiler
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 23, 2021

Dear @InfoSec ,

unfortunatelly there is no specific REST request for the amount of attachments of one page, but with GET /rest/api/content/{id}/child/attachment and reading the JSON entity "size" you get the number you want.

If not, you can still do pagination and count JSON array opjects.

So long

Thomas

Thomas Deiler
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 23, 2021

Note: If your array ends at 200 and you have more than 200 attachments, you discovered a bug ;)

Thomas Deiler
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 5, 2021

Dear @InfoSec ,

don't forget to press the "green accept answer" button to indicate other readers the successful answering of your question.

So long
Thomas

Gulrez Ahmed
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 19, 2021

I am also seeing that my array is ending at 200 even though I give a higher limit value.

How can this issue be resolved if this is a bug?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events