What HTTP code to use in the REST API for a subresource if the parent resource does not exist?

My team and I are coding a REST API, but some concepts are still not fully understood.

At a given resource: objective/{id}/goal

where target is a collection

If the user tries to reach a goal that doesn't exist, the API will return a status code 404

that is pretty simple.

ex: objective/999

returns404

For some reason, the consumer is trying to get targets from this non-existent resource:

ex: objective/999/goal

returns?

What is the most appropriate return code? I have a feeling that it should be too 404

. Some people are considering a different error code because the API has to communicate in some way that the parent resource does not exist in the first place.

+3


source to share


1 answer


Use a 404. Be aware that a 404 response may contain a response body. Therefore, you can answer something like this:

Request
GET /objective/7/goal

Response
404 Not Found
{
    "type": "ParentNotFound",
    "description": "The parent resource was not found.",
    "parent_uri": "/objective/7"
}

      



In general, it's a good idea to include some sort of response organ for error status codes. Even when the error status code is used in a standard way, it's still nice because the API client sees a human message about why I'm seeing the error. The benefit is even greater when the error status code is used almost standardly, but slightly off.

+6


source







All Articles