Composite key for REST API methods

As per general REST standards, each endpoint exposes a resource, and client can work on them with http verbs. In this example your resource is vehicle, and client is fetching data from server using GET. Ideally, each resource should be uniquely identified with a unique (single) key.

But your resource (vehicle) does not have a single value unique key, and it cannot be changed in the system! In this case you can still make the GET call with all required parameters to identify the resource, like any other standard http calls, like

GET /vehicles?type=Car&color=Red&transmission=Automatic&manufactureYear=2008

The technology/platform you are using, if that allows making custom routes for your method, you can create a custom route something like

new route("/vehicles/{type}/{color}/{transmission}/{manufactureYear}")

And call your service as

GET /vehicles/Car/Red/Automatic/2008

The good thing about this is, your uri becomes shorter. But on the other hand [1] For all methods/resources of this type, you’ll have to create custom routes, and [2] this uri doesn’t make much sense unless you have knowledge of the specific method and route.

Leave a Comment

File not found.