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?
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.
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 thenext
URL present in theLink
response header.
Regards, Roman
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Note: If your array ends at 200 and you have more than 200 attachments, you discovered a bug ;)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.