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

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

    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.

    0 讨论(0)
  • 2020-12-02 03:57

    My two cents: methods who spend their time changing from plural to singular or viceversa are a waste of CPU cycles. I may be old-school, but in my time like things were called the same. How do I look up methods concerning people? No regular expresion will cover both person and people without undesirable side effects.

    English plurals can be very arbitrary and they encumber the code needlessly. Stick to one naming convention. Computer languages were supposed to be about mathematical clarity, not about mimicking natural language.

    0 讨论(0)
  • 2020-12-02 03:57

    Have a look at Google's API Design Guide: Resource Names for another take on naming resources.

    In short:

    • Collections are named with plurals.
    • Individual resources are named with a string.
    |--------------------------+---------------+-------------------+---------------+--------------|
    | API Service Name         | Collection ID | Resource ID       | Collection ID | Resource ID  |
    |--------------------------+---------------+-------------------+---------------+--------------|
    | //mail.googleapis.com    | /users        | /name@example.com | /settings     | /customFrom  |
    | //storage.googleapis.com | /buckets      | /bucket-id        | /objects      | /object-id   |
    |--------------------------+---------------+-------------------+---------------+--------------|
    

    It's worthwhile reading if you're thinking about this subject.

    0 讨论(0)
  • 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 ;).

    0 讨论(0)
  • 2020-12-02 04:02

    Plural

    • Simple - all urls start with the same prefix
    • Logical - orders/ gets an index list of orders.
    • Standard - Most widely adopted standard followed by the overwhelming majority of public and private APIs.

    For example:

    GET /resources - returns a list of resource items

    POST /resources - creates one or many resource items

    PUT /resources - updates one or many resource items

    PATCH /resources - partially updates one or many resource items

    DELETE /resources - deletes all resource items

    And for single resource items:

    GET /resources/:id - returns a specific resource item based on :id parameter

    POST /resources/:id - creates one resource item with specified id (requires validation)

    PUT /resources/:id - updates a specific resource item

    PATCH /resources/:id - partially updates a specific resource item

    DELETE /resources/:id - deletes a specific resource item

    To the advocates of singular, think of it this way: Would you ask a someone for an order and expect one thing, or a list of things? So why would you expect a service to return a list of things when you type /order?

    0 讨论(0)
  • 2020-12-02 04:03

    From the API consumer's perspective, the endpoints should be predictable so

    Ideally...

    1. GET /resources should return a list of resources.
    2. GET /resource should return a 400 level status code.
    3. GET /resources/id/{resourceId} should return a collection with one resource.
    4. GET /resource/id/{resourceId} should return a resource object.
    5. POST /resources should batch create resources.
    6. POST /resource should create a resource.
    7. PUT /resource should update a resource object.
    8. PATCH /resource should update a resource by posting only the changed attributes.
    9. PATCH /resources should batch update resources posting only the changed attributes.
    10. DELETE /resources should delete all resources; just kidding: 400 status code
    11. DELETE /resource/id/{resourceId}

    This approach is the most flexible and feature rich, but also the most time consuming to develop. So, if you're in a hurry (which is always the case with software development) just name your endpoint resource or the plural form resources. I prefer the singular form because it gives you the option to introspect and evaluate programmatically since not all plural forms end in 's'.

    Having said all that, for whatever reason the most commonly used practice developer's have chosen is to use the plural form. This is ultimately the route I have chosen and if you look at popular apis like github and twitter, this is what they do.

    Some criteria for deciding could be:

    1. What are my time constraints?
    2. What operations will I allow my consumers to do?
    3. What does the request and result payload look like?
    4. Do I want to be able to use reflection and parse the URI in my code?

    So it's up to you. Just whatever you do be consistent.

    0 讨论(0)
提交回复
热议问题