Gino Pane
Gino Pane

Reputation: 5011

Which HTTP code to choose when parent resources are missing

Let's consider the following method:

GET https://server.com/resource1/{id1}/resource2/{id2}/resource3/{id3}/target/{targetId}

So basically we are getting a target. If the target with targetId does not exist, we respond with 404 and this is fine. But what about previous resources?

What if resource1, resource2, resource3 do not exist?

Is it 404 again? Or is it 400 (not entirely applicable due to HTTP spec though). Or maybe even 424 (because dependent resources are missing)?

I understand there would be no 100% correct answer probably. I'm also aware of REST HTTP status codes for failed validation or invalid duplicate

Upvotes: 0

Views: 101

Answers (1)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57214

If the document (resource) identified by /resource1/{id1}/resource2/{id2}/resource3/{id3}/target/{targetId} doesn't have a current representation, then you should return 404.

It doesn't matter whether the resource doesn't exist because a lookup based on id1 failed, or a lookup based on id2 failed, or a lookup based on id3 failed, or a lookup on targetId failed.

You can think of a URI as cache key: does this spelling match a resource that has a current representation?

The message-body of the response is available if you want to explain this specific problem. But status codes and headers are metadata in the "transfer of documents over a network" domain; general purpose components don't care about our implementation details.

Upvotes: 1

Related Questions