Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a resource count in a REST API?

Let's say I have a REST API, which has basic methods to retrieve users and the photos of a user. For example:

// Get a user:
GET /user/123

// Get the photos of a user:
GET /user/123/photos

// Get a photo:
GET /photo/789

This is quite straightforward, however now I also need a method to retrieve the number of photos for a particular user. I don't want to retrieve all the photos because that would slow everything down and is not necessary. What would be the best way to do that in a REST API?

I thought about implementing something like GET /user/123/photo_count however "photo_count" is not a resource so that doesn't seem right.

How would I go about presenting this kind of information properly in a REST API?

like image 800
laurent Avatar asked Dec 21 '22 11:12

laurent


1 Answers

// Get the photos of a user: 
GET /user/123/photos

This does not have to actually return the photos, it could return just a list of links.

It could even be a partial list of the first n links with information on the total number, and links to get the next/prev batch.

like image 144
Szocske Avatar answered Dec 29 '22 04:12

Szocske