3

Similar to HTTP status code 500-Internal Server Error,200-OK,201 Created etc... Is there any code for "Already Existing" to be giver as response from server, While trying to create a new object (if the object with same values exists) ??

dany
  • 1,801
  • 7
  • 27
  • 40
  • 'Already exists' by what semantic? Identical ID? Similar content? –  Nov 06 '12 at 12:11
  • 1
    @Tichodroma 'Already Exists' like object having a similar email_id,contact_number etc... which are meant to be 'unique' – dany Nov 06 '12 at 12:24
  • 2
    Possible duplicate of [REST HTTP status codes](http://stackoverflow.com/questions/3290182/rest-http-status-codes). In this case, you almost certainly want `409 CONFLICT`. – dkarp Aug 10 '13 at 15:41

1 Answers1

1

If your client sends an If-None-Match-header like described here:

The meaning of "If-None-Match: *" is that the method MUST NOT be performed if the representation selected by the origin server [...] exists, and SHOULD be performed if the representation does not exist

Then if the same resource exists, you can respond with a 412 Precondition Failed:

if "*" is given and any current entity exists for that resource, then the server MUST NOT perform the requested method, unless required to do so because the resource's modification date fails to match that supplied in an If-Modified-Since header field in the request. [...] the server MUST respond with a status of 412 (Precondition Failed).

Instead of * (which means "if anything exists") you can also use an Etag, which basically is a checksum of the entity that is calculated by the server. You can detect identical entities by identical Etags.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272