3

I am confused whether we should create separate API for fetching result and result-count OR we should fetch count based on query string in the result API only.

/api/results/ : Fetches all records
/api/results/?city=1: Fetches all records with city=1
/api/results/?iscount=1: Fetches the count of records i.e. list of cityId and count of record for respective cityId
/api/results/?city=1&iscount=1: Fetch the count of record for cityId=1

OR

/api/resultcount/: Fetches the count of records i.e. list of cityId and count of record for respective cityId
/api/resultcount/?city=1: Fetch the count of record for cityId=1

To me query string is used for filtering of resource so, I am in favor of creating separate API for fetching the counts. Opinion?

Tomm
  • 1,021
  • 2
  • 14
  • 34
Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98

1 Answers1

2

We don't need to create another point for fetching the count. Instead, we can send the count details in the response header.

It will be something like below,

/api/results/(GET method) - This will return the results.

/api/results/(HEAD method) - This will just return the results count in the response header.

Please take a look at the following link

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Balachandar
  • 392
  • 5
  • 8
  • 4
    The problem with the `HEAD /api/results` is that the server-side query will have to actually fetch the results, when all you want is a count (less intensive query). – pedromorfeu Nov 30 '18 at 12:48