I have a normal, basic REST api like:
/
GET - list
POST - create
/<id>
GET - detail
PUT - replace
PATCH - patch
DELETE - delete
When a POST comes in to /, I usually create an object and make a new id. Some (one) of the fields are (is) required to be unique. So, a POST with such duplicate data could result in:
- 500 – IntegrityError
- Make it more like a
PUT/PATCHto/<id>and update the existing record - Catch/avoid the error and return some sort of
4XX - Something else I’m not thinking of.
1 seems out: the request is either bad or I can deal with it. What is the correct way to handle this situation?
@StevenFisher is correct. 409 Conflict is the correct response.
For instance, a GET on / might tell a client that they can create users as follows
Following the hypermedia control and trying to create a user with the username “Skylar Saveland” might result in
Similarly, trying to create a user without a password might result in
or you might have multiple errors, e.g.,
NOTE: An appropriate media type will need to be created to go along with the above, which will explain the structure of the hypermedia controls (including the error attributes on the forms) and define the meaning of the various element names (e.g., users, username, password, etc).