REST URI convention - Singular or plural name of resource while creating it

前端 未结 22 2156
长情又很酷
长情又很酷 2020-12-02 03:35

I\'m new to REST and I\'ve observed that in some RESTful services they use different resource URI for update/get/delete and Create. Such as

  • Create - using
22条回答
  •  离开以前
    2020-12-02 03:57

    To me plurals manipulate the collection, whereas singulars manipulate the item inside that collection.

    Collection allows the methods GET / POST / DELETE

    Item allows the methods GET / PUT / DELETE

    For example

    POST on /students will add a new student in the school.

    DELETE on /students will remove all the students in the school.

    DELETE on /student/123 will remove student 123 from the school.

    It might feel like unimportant but some engineers sometimes forget the id. If the route was always plural and performed a DELETE, you might accidentally wipe your data. Whereas missing the id on the singular will return a 404 route not found.

    To further expand the example if the API was supposed to expose multiple schools, then something like

    DELETE on /school/abc/students will remove all the students in the school abc.

    Choosing the right word sometimes is a challenge on its own, but I like to maintain plurality for the collection. E.g. cart_items or cart/items feels right. In contrast deleting cart, deletes the cart object it self and not the items within the cart ;).

提交回复
热议问题