MNY
MNY

Reputation: 1536

What http result should I return for duplicate entry

I have an api that let users create a person object and im creating it in the mysql db, currently when mysql returns a MySQLException if t.errorMessage.errorCode == 1062 i return the user 400 with message that a person with this id already exist, should I instead return 409?

Upvotes: 5

Views: 6075

Answers (1)

cassiomolin
cassiomolin

Reputation: 131017

First of all, keep the following in mind (quoted from the chapter 6 of Fielding's dissertation that defines the REST architectural style):

The resource is not the storage object. The resource is not a mechanism that the server uses to handle the storage object. The resource is a conceptual mapping [...]

Abstracting the database concerns, if there's a conflict in the state of the resource, the 409 status code seems to be the most suitable code for this situation. The server also should return details about the conflict in the response payload:

6.5.8. 409 Conflict

The 409 (Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource. This code is used in situations where the user might be able to resolve the conflict and resubmit the request. The server SHOULD generate a payload that includes enough information for a user to recognize the source of the conflict. [...]

Upvotes: 6

Related Questions