Search code examples
restrestful-architectureapi-designrestful-url

RESTful using the same endpoint with id or other parameter is a good practice?


Scenario

I have a case when I want get something by id, common case, and some times by date.

Eg.:

/companies/:id/tickets/:id
/companies/:id/tickets/:date

From my studies this is not a good practice but and here come my doubt about it.

Should I use the endpoint without the id and use the payload to get the date?

Eg.:

/companies/:id/tickets

This should return all tickets from that company right? So getting some date data from the payload and use that as a filter the return don't seems right to me. My endpoints would not be honest about what they are doing if I do that.

What RESTful practice I should use here?


Solution

  • /companies/:id/tickets/:id - this is fine for getting a single resource.

    I think most people would agree filtering and searching resources would be best done with query parameters. In fact, the JSONAPI spec has a recommendation regarding it specifically.

    Read up on http://jsonapi.org/recommendations/#filtering and the other recommendations, they cover the most common use cases such as yours.

    You can also have a standalone tickets resource and implement the company id filter for it as well as, in general, having multiple URLs for the same resource(s) is perfectly fine.