I am building RESTful API and I have come across a situation. During user sign up, if the email already exists then between 422
and 409
which http response code makes sense?
I have browsed through similar one and the accepted answer is from the year 2012. Does the answer still hold good? Examples would be of great help.
A 422 status code occurs when a request is well-formed, however, due to semantic errors it is unable to be processed. This HTTP status was introduced in RFC 4918 and is more specifically geared toward HTTP extensions for Web Distributed Authoring and Versioning (WebDAV).
The appropriate status code for "Already Exists" would be '409 Conflict'.
Error 422 is an HTTP code that tells you that the server can't process your request, although it understands it. The full name of the error code is 422 “unprocessable entity.” In a nutshell, the error means that you're making a request the server understands, but it can't process it.
You may not find a very definitive answer to this question, once both 409
and 422
would be suitable for this situation (I would go for 409
though).
For any of them, you must ensure that a payload describing the problem is sent back to the client.
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. [...]
11.2. 422 Unprocessable Entity
The
422
(Unprocessable Entity) status code means the server understands the content type of the request entity (hence a415
(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a400
(Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.
I think 409
is most appropriate in this described example as the request is conflicting with an already existing registration.
For example, if the service couldn't accept a .de domain based email address; 422
seems preferable. This example would also not qualify for a 400
as a .de domain would be valid syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With