Guys, I'm coding a blog and sometimes I don't want to fetch the whole data from the post.
For instance: I'm coding the "Blog" page, which only shows a set of posts per page: only their title, image, post excerpt and URL to the post page itself - _So I don't want to fetch the content of the post_.
#Expected
{title: "The needed banana",
excerpt: "I just wanted the banana",
url:"#",
image: "banana_dancing.gif"}
#Fetched
{title: "The needed banana",
excerpt: "I just wanted the banana",
content: "I just wanted the banana, but I got the whole forest!",
url:"#",
image: "banana_dancing.gif"}
Do you guys know how to do it?
Also, how can I manage to fetch the data with pagination. If it is not possible, how can I retrieve only the total number of collection items (so I can code the pagination myself)?
To the guys looking for more information on how to use the API, I didn't realize the body arguments on the documentation at first, so if you're looking for them, they are: "skip" (offsets data), "limit", "sort" and "populate" (which I don't understand what is it's purpose).
For example: "/api/collections/get/posts?token=xxtokenxx&sort[_created]=-1&limit=2&skip=2"
try {"fields":{"title":true,"excerpt":true,"url":true,"image":true,"_id":false}} as a post request for fetching specified fields.
{
"fields":
{
"title":true,
"excerpt":true,
"url":true,
"image":true,
"_id":false
}
}
If you don't add "_id":false, the _id field will always be sent automagically.
I didn't try the pagination yet.
"populate" means fetching nested data. If your entry has a collection link, and the linked entry has an asset and another collection link and so on, you would get a lot of data with a high population value.
@raffaelj It worked. Thank you!
Also, to those looking for fetching only the number of entries, (drums preparing for terrible solution): limit the fetch to one entry, it will collect the total number of entries without fetching all of them. Forces you to get one but at least doesn't obligate you to use a lot of processing and memory.
For just counting there is a simpler solution by using a custom API endpoint. I added the one for counting to my personal collection here.
<?php
/*
return only the count of a collection
place this file in /cockpit/config/api/collections/count.php
call it with https://urltocockpit.de/api/collections/count?token=mysecrettoken&collection=mycollection
or call it with https://urltocockpit.de/api/collections/count?token=mysecrettoken
and send json like : {"collection":"mycollection"}
*/
return $this->module('collections')->count($this->param('collection'));
Wouldn't it be nice if the total count came through as a header? Then you could just make a HEAD request
@victormoretti I read your question again. If you didn't find it out by yourself until now - the arguments limit and skip are for pagination.
get 5 entries from 1 to 5: {"limit":"5"}
get 5 entries from 6 to 10: {"limit":"5","skip":"5"}
get 10 entries from 21 to 30: {"limit":"10","skip":"20"}
and so on.
With "I didn't try the pagination yet." I meant, that I didn't write an application with pagination or tried to automate anything in this direction.
Most helpful comment
For just counting there is a simpler solution by using a custom API endpoint. I added the one for counting to my personal collection here.