Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST design question multiple identifiers for a resource which need to be exposed to clients

Tags:

rest

http

I would like to ask a design question about a REST HTTP API that I am exposing.

I have to sometimes access a widget with ID 3:

http://ourserver/service/widgets/3

Get widget with ID 3

But I also sometimes need to access a widget by it's SKU#.

Is it wrong to expose the same resource via 2 URLS?

BUt I need my clients to get a widget by either it's ID or it's SKU.

Which of the following is better?

  • http://ourserver/service/widgets/bysku/skyunumber
  • http://ourserver/service/widgets/skyunumber?idtype=sku

Again to repeat, I need my clients to be able to look up a widget in 2 different ways. What is the best way to design the URLs?

like image 346
rest Avatar asked Nov 10 '10 21:11

rest


1 Answers

I would suggest considering the following:

GET http://ourserver/service/widgets?sku=34342323
=>
303 See Other
Location: http://ourserver/service/widgets/43

GET http://ourserver/service/widgets/43

By using a redirect, you can support any number of criteria to find widgets. The key issue to consider is what happens when you start to enable caching. If you return representations from multiple URLs you end up polluting the cache with multiple copies and it makes it much more difficult to invalidate the copies in the cache when you do updates.

like image 163
Darrel Miller Avatar answered Nov 18 '22 09:11

Darrel Miller