0

Take these two tables in a database:

  • topics that contains a list of topics
  • terms that contains a list of uni, bi and trigrams
  • baggings with columns topic_id, term_id and weight

A term can be used in several topics so how would you build the API endpoints? It's a query params vs path params...

Path Params

GET https://localhost/topics/:topic-id/terms/

Query Params

GET https://localhost/terms/?filter_by=topic&filter_value=:topic-id

The last solution can also be useful to perform various terms operations and searches. The first (Path Params) can only be used in relation to the given topic.

Lazhar
  • 1,223
  • 12
  • 28
  • 1
    Possible duplicate of [REST API Best practices: Where to put parameters?](http://stackoverflow.com/questions/4024271/rest-api-best-practices-where-to-put-parameters) – jmattheis Jan 28 '17 at 08:42

1 Answers1

2

You can support both if you want to. I usually use query parameters when I need to search for something with dynamic parameters.

In order to fetch all terms associated with a topic id, I would go with

GET https://localhost/topics/:topic-id/terms/

This should list all terms for the topic_id

But in case your search had more filters you wanted to use, I would go ahead and support query parameters. Say you want only terms which match a certain type for a particular topic, then I would use the query parameters.

I personally don't prefer to lookup a specif topic-id like this.

GET https://localhost/terms/?filter_by=topic&filter_value=:topic-id
Vishnu J
  • 491
  • 2
  • 10