REST - supporting multiple possible identifiers

后端 未结 5 1232
一向
一向 2020-12-13 02:00

For the site I am working on, we are in the process of improving our URLs for one type of resource - specifically, moving away from numerical IDs toward unique, descriptive

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 02:27

    Your first option is probably the best.

    Searching for users by ID:

    /users/id/48573
    

    Searching for users by short name:

    /users/name/thisisausername
    

    If they leave out that path parameter, you could always default to your new short username format.

    Another option that I have seen quite a bit is to use query parameters like the following:

    /users?id=48573
    /users?name=thisisausername
    

    I think the first looks a bit cleaner and more readable.

提交回复
热议问题