I'm creating a RESTful API for creating users that enforces unique email addresses:
Successful POST /users
: HTTP 201 Created
If I POST
the same email address again, what should the response code be? Is 409 Conflict
the appropriate response code?
The HTTP Status Code 207 means that the message body that follows is by default an XML message and can contain a number of separate response codes, depending on how many sub-requests were made.
The appropriate status code for "Already Exists" would be '409 Conflict'.
The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.
The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.
Yes, 409 is the most appropriate response code here. Even though you are most likely returning 201 on success, you're still POSTing to a resource which is described as a collection, and POSTing a duplicate email is definitely a conflict with "the current state of the resource" as a collection. You should return a response body with a description of the problem, and hyperlinks to help resolve the problem, if possible.
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