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

前端 未结 22 2200
长情又很酷
长情又很酷 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 04:08

    Use Singular and take advantage of the English convention seen in e.g. "Business Directory".

    Lots of things read this way: "Book Case", "Dog Pack", "Art Gallery", "Film Festival", "Car Lot", etc.

    This conveniently matches the url path left to right. Item type on the left. Set type on the right.

    Does GET /users really ever fetch a set of users? Not usually. It fetches a set of stubs containing a key and perhaps a username. So it's not really /users anyway. It's an index of users, or a "user index" if you will. Why not call it that? It's a /user/index. Since we've named the set type, we can have multiple types showing different projections of a user without resorting to query parameters e.g. user/phone-list or /user/mailing-list.

    And what about User 300? It's still /user/300.

    GET /user/index
    GET /user/{id}
    
    POST /user
    PUT /user/{id}
    
    DELETE /user/{id}
    

    In closing, HTTP can only ever have a single response to a single request. A path is always referring to a singular something.

提交回复
热议问题