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

前端 未结 22 2188
长情又很酷
长情又很酷 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:53

    An id in a route should be viewed the same as an index to a list, and naming should proceed accordingly.

    numbers = [1, 2, 3]
    
    numbers            GET /numbers
    numbers[1]         GET /numbers/1
    numbers.push(4)    POST /numbers
    numbers[1] = 23    UPDATE /numbers/1
    

    But some resources don't use ids in their routes because there's either only one, or a user never has access to more than one, so those aren't lists:

    GET /dashboard
    DELETE /session
    POST /login
    GET /users/{:id}/profile
    UPDATE /users/{:id}/profile
    

提交回复
热议问题