Identify item by either an ID or a slug in a RESTful API

前端 未结 1 1891
慢半拍i
慢半拍i 2021-02-03 22:14

I\'m currently designing an API and I came a cross a little problem: How should a URL of a RESTful API look like when you should be able to identify an item by either an

1条回答
  •  北海茫月
    2021-02-03 22:28

    Of the three I prefer the third option, it's not uncommon to see that syntax; e.g. parts of Twitter's API allow that syntax: https://dev.twitter.com/rest/reference/get/statuses/show/id

    A fourth option is a hybrid approach, where you pick one (say, ID) as the typical access method for single items, but also allow queries based on the slug. E.g.:

    GET /items/
    GET /items?slug=
    GET /items?id=
    

    Your routing will obvious map /items/id to /items?id=

    Extensible to multiple ids/slugs, but still meets the REST paradigm of matching URIs to the underlying data model.

    0 讨论(0)
提交回复
热议问题