Torvic
Torvic

Reputation: 83

CreateOrUpdate endpoint on DDD

I have a rest API for an application that I am developing. One of the endpoints receives the name, email and phone fields to create a new user (without password). Obviously the endpoint would be /users [POST] Would it be correct to take advantage of this endpoint to, if the user already exists, update it with the new data? Or is it better to create a different endpoint (PUT) to update the user? If so I would have to put the business logic outside of this API, and I don't like that idea.

Upvotes: 0

Views: 156

Answers (1)

user17618823
user17618823

Reputation:

This question is not related to DDD, as DDD does not provide guidance on API design.

But to answer your question, whether or not you should use PUT or POST will depend on whether or not the action should be idempotent.

POST is typically used to create a new resource

POST is not idempotent, if the same request is sent multiple times there will be different results (new resource gets created each time). The same request sent to POST /users will create a new resource each time.

PUT is used to either create or replace an existing resource (not necessarily update).

The PUT method is idempotent, so if the same request is sent multiple times it will be the same as if it is sent once. The same request sent to PUT /users/1 will have the same result.

If you want to update part of the resource (update rather than replace), you can use PATCH.

Upvotes: 2

Related Questions