Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP response code when resource creation POST fails due to existing matching resource

Imagine we have an API where a new Employee can be created through a POST to

www.example.com/api/employees

An employee could be described as,

{
  name: "John Smith",
  tax_number: "ABC123"
}

The tax number is universally unique for all humans. If a create were made, and a record already existed in which the name and tax number matched an existing record, it is safe to assume the requester would like a reference to that record returned (with it's internal id and other data the client may not have, eg. created at, updated at).

What would the HTTP status code be for returning that resource? I suppose a redirect could be used with a return of just the id, but I'd prefer to enclose the entire object in the response.

This case is unique to a simple duplicate error, in the sense that if a duplication is attempted it means the record you wish to create already exists- as opposed to it conflicting with an existing record.

like image 231
dave Avatar asked Aug 28 '14 05:08

dave


People also ask

What does HTTP response code 400 refers to?

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).

When should HTTP 404 be returned?

When a user requests a nonexistent URL on your website, you should return an individual error page that lets them know that the requested URL does not exist. You should also make sure that the server returns the correct HTTP status code “404“.

What is a 201 status code?

What Is a 201 Status Code? The request has been fulfilled and has resulted in one or more new resources being created. The primary resource created by the request is identified by either a Location header field in the response or, if no Location field is received, by the effective request URI.

What HTTP response code indicates a resource is not found?

The HTTP 404 Not Found response status code indicates that the server cannot find the requested resource.


2 Answers

I think if you Post a resource to an API, you expect a code 201 that will state that the resource was created, and the body will (can) contain the resource. Otherwise you first have to check if the resource is already created, but this will need two calls.

otherwise, you have the status code 409 :

The request could not be completed due to a conflict with the current state of the resource.

[Edit] after reading on Restfull Web Apis from amundsen, Ruby and Richardson

Best would be to use the 409 status code that states that there is a conflict with a resource that exists on the server. Put the url of that "conflicted" resource in the Location Header of the response and add a message of the conflict in the body

like image 150
Cedric Dumont Avatar answered Oct 28 '22 15:10

Cedric Dumont


There is a 409 Conflict response code, that is commonly used to let the client know such entity already exists on the server. It might not be very intuitive to have to parse the body of an error response but if you specify this in your API documentation there shouldn't be a problem (to put the info about the original entry in the response body).

like image 39
stan0 Avatar answered Oct 28 '22 17:10

stan0