Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST URI convention - 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?

like image 588
JPReddy Avatar asked Jul 27 '11 14:07

JPReddy


People also ask

Should REST URL be plural or singular?

Using nouns for naming URIs is a REST API naming best practice, but when should you use singular or plural nouns? In general, using plural nouns is preferred unless the resource is clearly a singular concept (e.g. https://api.example.com/users/admin for the administrative user).

Should API path be singular or plural?

I suggest using singulars for everything. If there is an /{id}, then the API should return a single record. If there is not an /{id} that follows, then API should return the collection.

Is resource singular or plural?

Declaring a resource or resources generally corresponds to generating many default routes. resource is singular. resources is plural.

How should we name our resources?

In deciding what resources are within your system, name them as nouns as opposed to verbs or actions. In other words, a RESTful URI should refer to a resource that is a thing instead of referring to an action. Nouns have properties as verbs do not, just another distinguishing factor.


1 Answers

For me is better to have a schema that you can map directly to code (easy to automate), mainly because code is what is going to be at both ends.

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)  
like image 110
jla Avatar answered Sep 30 '22 13:09

jla