Valentyn Zakharenko
Valentyn Zakharenko

Reputation: 3088

Is it OK to use a 409 HTTP code in the case when the record with specified property that should be unique already exists?

I have a table that contains two unique columns. Is it OK to return 409 for PUT requests that trying to create a record with a duplicate column value?

Upvotes: 4

Views: 7097

Answers (1)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57289

Is it OK to use a 409 HTTP code in the case when the record with specified property that should be unique already exists?

Sure.

It may help to consider that status codes are meta data; they are information provided to inform general purpose components coarse grained understanding of the nature of the HTTP response, rather than being information for the API consumer.

If you like, consider the common case on the web: we send a 404 response so that the browser (and any general purposes caches along the way) will understand that the body of the response is a representation of a description of a client error, so that they can manipulate their local caches correctly.

But the human being gets an HTML representation of a fail whale.

Given that the primary audience of the status code is machine, rather than man, you can get a sense of the appropriateness of a status code by looking into what the consequences of the different codes would be.

And the truth is, for many of the codes... 400 vs 403 vs 409 vs whatever... there just isn't a lot that the machines can do that is different from one code to the next. In a lot of cases, about the best you could hope for is to highlight the part of the http request (method-token, URI, HTTP-version, body, authorization...) that is the source of the problem.

409 is a fine choice for your case - in particular, it calls attention to the body of the request, informs that there is some incompatibility between the semantics of the current request and the current state of the resource, and indicates that the client may be able to resolve the conflict and resubmit the request.

But if you were to choose "403" instead, the negative consequences would be really small. From the outside, in practice, is just doesn't matter very much.

Upvotes: 8

Related Questions