5

Let say I have a resource Person and on a person I have a nested resource SomeResource. Which status code should be returned and why in the following scenarios?

/Person/1/SomeResource (SomeResource does not exist)
/Person/1/SomeResource (Person does not exist)

I think that 404 should be used in both scenarios but not everyone agrees and I would like to know why. You could argue that Person is the underlying resource and SomeResource is just a "property" on that resource and the request to SomeResource should therefor return nothing in the first scenario but 404 in the second scenario. I think that might be an option, but I still like 404 more. Another scenario that I don't like at all is to return 500 with an error description, that was an alternative I heard in the discussion as well but that forces the consumer to program against exception which I don't like. 500 for me means that something went wrong and you can't really do anything about it.

The problem is that the argument was that you don't know why you got a 404 if you got it, was it because of that the Person didn't exist or because of that the SomeResource didn't exist.

UPDATE 1: Maybe I should break out SomeResource to a separate resource like

/SomeResource/1

which returns a response like {data: {the data}, person: {person data}}, and this only returns 404 if both are missing but if the data is missing a 200 is returned with empty data.

UPDATE 2: I think I figured out which status code to use and it is 400 for when the person is not existing since I at that point considering it a request you should never have done, that is, a bad request. When SomeResource is missing I would go for 404 since the Person did exist but the nested resource was missing.

1 Answer 1

1

It may be helpful to remember that a given URI is intended to identify at single "resource". Semantically, a URI structure does not support the concept of "nested" resources. It seems you have two resources: Person & SomeResource in your scenario where SomeResource has a relation of some sort with Person. You could try a structure like this to represent this relationship:

GET /person/1

{
    name: "Some Value",
    someResource: {
        "Href": "http://yourSite.com/someresource/123",
        "Title": "Get some resource for this specific person"
    },

    // other key/value pairs as appropriate...
}

This way you are not overloading the 400 & 404 with application specific meanings. If the client has received a valid Person result, it would simply invoke the SomeResource href and would receive the appropriate 404 or not depending on SomeResource 123 existing. If the Person URI does not exists, invoking it would appropriately return 404 since it does not exist.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't agree with you that "a URI structure does not support the concept of "nested" resources." Of course it does, the question shows that the URI structure is perfectly valid and therefor it is supported. An URI should be an identifier for a resource, if it is nested or not doesn't really matter I think, as long as it identifies the resource. I don't think that you are wrong, but I am not sure you are right either :).
That's the cool thing about Internet standards and the way HTTP works, one is free to use HTTP in a manner that suits ones purposes. If you want to overload the semantics of the URI, there is no mechanism that will prevent that. However, if you want to conform to these standards, look at the standards document that defines URIs. I could not see where there is any intension of supporting the notion of the "nested" resources. It's perfectly fine to agree to disagree, I don't want to start a "religious war" here. :)
No religious war going on here, I just want to understand and appreciate any feedback I get. And when reading through the standards it looks like you might be correct if one want to stick to the standards 100%. The thing we should have used is maybe a "fragment" which describe a nested resource.
Thanks! That's one of the reasons I like StackOverflow so much, the questions & answers get me digging into things I need to learn more about.
But at the same time, as you say in the first comment: "one is free to use HTTP in a manner that suits ones purposes." And I really think nested resources can be very useful, an example could be order/{id}/orderlines/{orderlineId}. And I don't thinkt that violates the part of identifying a single resource, it still does, as does my example in the question. That the resource is nested doesn't really matters, it is still a single identification mapped to a single resource. Thank you for your point of view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.