Filter fields on API response

Basically I want the same thing as mentioned here, How to filter fields on API response, but for Cockpit v2.

The old solution was to add the parameters to the request body like so:

{
  "fields": {
    "slug": true,
    "_id": false
  }
}

But this doesn’t seem to work anymore.

The docs state that you have to use Mongo Query syntax now. So I’ve tried just that by moving the fields to the requests parameters:

GET COCKPIT_HOST/api/pages/pages?fields={"title": true}

I’d expect the response to contain an array with just one field, title, per entry.
But I still get all fields in the response.

What’s the right way of doing this now in v2 :thinking:

Thanks in advance :v:

REST API CHEATSHEET

get posts content with title = post 1

http://cockpit.test/api/content/items/posts?filter={title:'post 1'}

get posts content with title = post 1 and category = catone

http://cockpit.test/api/content/items/posts?filter={title:'post 1',category:'catone'}

FIELDS

Only get title field

http://cockpit.test/api/content/items/posts?fields={title:1} 

Only get title and category field

http://cockpit.test/api/content/items/posts?fields={title:1,category:1}

Get All Field , exclude title field

http://cockpit.test/api/content/items/posts?fields={title:0} 

Get All Field , exclude title and category field

http://cockpit.test/api/content/items/posts?fields={title:0,category:0} 

SORT

asc

http://cockpit.test/api/content/items/posts?sort={title:'1'}

desc

http://cockpit.test/api/content/items/posts?sort={title:'-1'}

LIMIT AND SKIP

http://cockpit.test/api/content/items/posts?fields={title:1}&limit=100&skip=1

POPULATE

populate content and content link

http://cockpit.test/api/content/items/posts?populate=1

Combination

http://cockpit.test/api/content/items/posts?fields={title:1}&sort={title:'-1'}

More…

2 Likes

Thanks for your reply!

Unfortunately, this doesn’t work with the pages endpoint.
The fields parameter doesn’t have any effect at all.

Is this supposed to be like that?
I relly need a filter for the pages because this would allow me to lower the response size by sooo much … :confused:

I think the Pages Api limited reference to the API Playground:

Maybe just use
GET /pages/routes
for get slug

1 Like

The idea is good, but this doesn’t give me the page ID (I don’t mean the slug), which I need for further processing.

But from my understanding it would make total sense to provide the ability to use filter and field parameters, just as at the content endpoint, wouldn’t it? Especially because page datasets can become really big once you start to add some layout fields.

@artur Feature request please thanks? :grin:

Take a look at the latest development codebase:
releases/develop • Cockpit files :wink:

1 Like