Singular or plural name of resource while creating it

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 /resources with POST method (observe plural) at some places using /resource (singular)
  • Update - using /resource/123 with PUT method
  • Get - Using /resource/123 with GET method
  • I'm little bit confused about this URI naming convention. What should we use plural or singular for resource creation? What should be the criteria while deciding that?


    The premise of using /resources is that it is representing "all" resources. If you do a GET /resources , you will likely return the entire collection. By POSTing to /resources , you are adding to the collection.

    However, the individual resources are available at /resource. If you do a GET /resource , you will likely error, as this request doesn't make any sense, whereas /resource/123 makes perfect sense.

    Using /resource instead of /resources is similar to how you would do this if you were working with, say, a file system and a collection of files and /resource is the "directory" with the individual 123 , 456 files in it.

    Neither way is right or wrong, go with what you like best.


    对于我来说,最好有一个可以直接映射到代码的架构(易于自动化),主要是因为代码是两端都会发生的事情。

    GET  /orders          <---> orders 
    POST /orders          <---> orders.push(data)
    GET  /orders/1        <---> orders[1]
    PUT  /orders/1        <---> orders[1] = data
    GET  /orders/1/lines  <---> orders[1].lines
    POST /orders/1/lines  <---> orders[1].lines.push(data) 
    

    I don't see the point in doing this either and I think it is not the best URI design. As a user of a RESTful service I'd expect the list resource to have the same name no matter whether I access the list or specific resource 'in' the list. You should use the same identifiers no matter whether you want use the list resource or a specific resource.

    链接地址: http://www.djcxy.com/p/1084.html

    上一篇: 如何在HTTP POST请求中发送参数?

    下一篇: 创建资源时使用单数或复数名称的资源