Mixing the REST API is multiple and unique for different resources?

The plural form for REST api is more natural and more used, for example. /api/users or api/users/123 .

But for some resources it is not natural, for example:

  • /api/login - register one user
  • /api/profile - get a registered user profile

these resources will never be used for more than one object / model in my application.

On the other hand, I read that mixing multiple and single forms in resource names is not good practice ( http://pages.apigee.com/web-api-design-ebook.html ).

So, I think what to do:

  • use one for all
  • use plural for all (with some silly forms like /api/logins )
  • to be inconsistent and use the plural for almost all resources, some special resources are expected, such as /api/login or /api/profile , which are always used with the same object / model.

What is the best approach?

+7
source share
4 answers

There are no strict rules for defining a RESTful API, but what I read the most is that common sense should prevail.

Therefore option 3:

to be inconsistent and use the plural for almost all resources, some special resources are expected, such as / api / login or / api / profile, which are always used with the same object / model.

is the most logical. You should always guess the URL when you think: β€œI need resource X, what would that URL look like?”

+6
source

I am not saying that I prefer plurals, but if you intend to use plurals, you can reconcile your special unities as follows:

GET /api/forms/login is a GET /api/forms/login form. Using this perspective, login is the identifier of only one form in the form collection.

POST /api/forms/login is the POST /api/forms/login form.

GET /api/users/{id}/profile retrieves the profile of the specified user. This works in many cases, but doesn’t work for anonymity sites where the user’s identity must remain hidden even when viewing their profile, which can leave their user ID and real name.

GET /api/profiles/{id} separates the profile object from the user ID and will work for the site of anonymity.

Alternatively, you can write GET /api/users/current/profile or GET /api/sessions/current/profile , which skips a specific identifier, for example, in your message, since the server will respond with content related to the current user.

+3
source

REST (View State Transfer) is mainly for a single object and for executing CRUD. Therefore, using unity makes more sense to me. But in case you need to get a list, then the plural makes sense. For example:

You want to get the user, then / api / user / {id}

But if you want to get a list of users, you have / api / users

+2
source

What I saw in some projects that I have been working on over the years is that for most common operations, the special mode looks friendlier, for example, for endpoints for a user resource, there may be the following:

 GET /user --> retrieves all users GET /user/{id} --> retrieves a user with the given id POST /user --> inserts a new user (the user object will come in the request body) PUT /user/{id} --> updates a user with the given id (the user object will come in the request body) DELETE /user/{id} --> deletes the user with the given id 

These are common operations, when you have massive insert / update / delete operations, then plural numbers are better

 POST /users (the user objects will come in the request body) PUT /users/{listOfIds} (the user objects will come in the request body) DELETE /users/{listOfIds} 

GET / user and GET / users will be synonyms, these two will take query parameters to refine the results, for example,

 GET /users?status=active 
+2
source

All Articles