Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

422 or 409 status code for existing email during signup

Tags:

rest

http

api

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.

like image 460
curious_coder Avatar asked Jun 20 '18 11:06

curious_coder


People also ask

When should I use 422 status code?

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).

What status code if user already exists?

The appropriate status code for "Already Exists" would be '409 Conflict'.

Why 422 error code means?

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.


2 Answers

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 a 415 (Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (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.

like image 190
cassiomolin Avatar answered Oct 06 '22 00:10

cassiomolin


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.

like image 21
David G Avatar answered Oct 05 '22 23:10

David G