Question

I am working on designing the REST APIs. One of the POST request to create a room in a home takes an input of ID of the home and room name like :

POST  /webCall/development/construction
<?xml version="1.0"?><room><home id='110001'/><name>large bedroom</name></room>

So my question here is,suppose the home ID = 110001, doesn't exist in the system, In that case should 404(Resource not found) be the response code or 412(Precondition failed) be the response code.

Possible arguements.

Why 404 : The resource home with ID : 110001 is not found.
Why 412 : The precondition to create a room is that a home should exist,which fails here.

Please provide your suggestions with reasoning, that would help me to take right decision.

Was it helpful?

Solution

412 is incorrect for the reason given by Sotirios.

404 is incorrect because the problem is with the payload, not the addressed resource (URI).

You could use 400 (generic), 409 or 422.

OTHER TIPS

The HTTP spec says the following about the 412 status code

The precondition given in one or more of the request-header fields evaluated to false when it was tested on the server. This response code allows the client to place preconditions on the current resource metainformation (header field data) and thus prevent the requested method from being applied to a resource other than the one intended.

So this status code would make sense if you were sending an If-Match header with the entity tag of the resource you're trying to add to.

Assuming you're posting to something like below

/homes/some-identifier/rooms

you should probably be returning a 404, since the resource /homes/some-identifier/rooms does not exist.

If you are actually doing

POST  /webCall/development/construction
<?xml version="1.0"?><room><home id='110001'/><name>large bedroom</name></room>

then 409 would probably be the most appropriate. However, I recommend you change your API to POST to the actual home resource.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top