Ronan Quillevere
Ronan Quillevere

Reputation: 3889

REST url for unique resource (url with singular ?)

I have a webserver with some configuration properties and I want to be able to change them using a REST API.

Example

{
    "maxUsers" : 10,
    "refreshPeriodInMin" : 5
}

I would like to represent this with a "configuration" object. According to REST principle I believe the best way to do it is :

GET/PUT/POST/DELETE /configurations/{id}

But in my case I have only one configuration object and I do not like the idea of have to query

GET /configurations

just for one object.

Because there is only one object the easiest solution I found is to use id=default

Would give something like

GET /configurations/default

Another solution I though about would be to have one object per property. This would give

GET /properties/maxUsers

Problem with that solution is that you need to know the name of property to be able to query it. PLus you will make several queries if you have multiple changes to make.

Upvotes: 2

Views: 378

Answers (1)

djmorton
djmorton

Reputation: 616

Keep the resource singular if it truly represents a singular thing. If you will only have one, there is no reason not to simply PUT to that resource when you want to create or update it, and GET from that resource when you want to retrieve it.

If it can not be deleted, return a 405 METHOD NOT ALLOWED on DELETE requests. If it can be deleted, a DELETE request to that resource is acceptable, after which GET requests can return a 404 NOT FOUND.

In many ways, adding an id element to the path like /configuration/default would probably confuse users because they might expect that they would be able to POST new configurations to /configuration in addition to the default one.

The key is to do something that is sensible and intuitive to consumers of the API.

Upvotes: 1

Related Questions