Adam Siemion
Adam Siemion

Reputation: 16039

Upsert (update or insert) of resources with generated URIs in REST?

My data access layer supports the upsert operations (Spring Data's CrudRepository.save()), when the id field is not set a new record is inserted and when the id field is set it is updated.

I am wondering how to justify the additional effort required to create two separate REST methods (both on the backend and the frontend side):

over creating just one REST method:

Are there any benefits of having two seperate methods other than being more RESTful?

Upvotes: 1

Views: 1706

Answers (2)

Dmytro
Dmytro

Reputation: 582

It's more clearer to using different resources for different actions. By REST ideology - PUT Idempotent method and POST not Idempotent. So each POST request should return new item in response, but PUT should return the same item in response.

I think it's a good when behavior is expected.

But if you write documentation for your api, you can using one resource for both actions (but I would choose the first option).

Upvotes: 2

user5547025
user5547025

Reputation:

The data access layer supporting upsert does not mean that the REST API should support it, too. I would not overload POST /users with two meanings. Stick to two different resources.

Edit:

POST /users has the general meaning of 'add a new instance to the resource collection'. I see no way to extend this to '... or update existing instance if an ID is given'. /users is the collection resource, not a single resource you want to manipulate. Doing a POST to a collection resource should manipulate the collection, not a single existing resource.

Upvotes: 0

Related Questions