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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With