title: REST HTTP response code for missing parent resource
body: <p><strong>My question is, should I return a HTTP response code of 400 Bad Request, 404 Not Found or 410 Gone when a parent resource, or higher, does not exist / has been deleted?</strong></p>
  <p>Looking for some guidance around how to handle a missing links in a RESTful resource tree, as I've read quite a bit but not a whole lot of personal experience.</p>
  <p>Say we have the resources structure below:</p>
  <pre><code>/users/{userId}/accounts/{accoundId}/logs/{logId}
  </code></pre>
  <p>One user can have many accounts, which in turn can have many orders, which in turn can have many logs. Logs only exist against a single account, and accounts only exist against a single user. Logs cannot exist without an account, and accounts cannot exist without a user.</p>
  <p>My problem comes when I try to resolve the below:</p>
  <pre><code>/users/123/accounts/321/logs - POST
  /users/123/accounts/321/logs/987 - GET
  /users/123/accounts/321/logs/987 - PUT
  /users/123/accounts/321/logs/987 - DELETE
  /users/123/accounts/321/logs/987 - PATCH
  /users/123/accounts/321/logs/987 - HEAD
  </code></pre>
  <p>But this resource does not, or no longer, exists:</p>
  <pre><code>/users/123/accounts/321
  </code></pre>
  <p>or this resource does not, or no longer, exists:</p>
  <pre><code>/users/123
  </code></pre>
  <p>I could argue it's a <strong>400 Bad Request</strong>, which as per <a href="https://www.rfc-editor.org/rfc/rfc7231" rel="nofollow noreferrer">RFC7231</a>:</p>
  <blockquote>
  <p>6.5.1.  400 Bad Request</p>
  <p>The 400 (Bad Request) status code indicates that the server cannot
  or    will not process the request <strong>due to something that is perceived
  to be    a client error</strong> (e.g., malformed request syntax, invalid
  request    message framing, or deceptive request routing).</p>
  </blockquote>
  <p>Would be by definition true, except in the scenario that a cache hadn't expired, meaning the application had yet to re-traverse the hierarchy, and another application had deleted the parent resource. By providing a relevant oplock, the client will have proven that as per its last knowledge, it was making a semantically correct request.</p>
  <p>I <em><strong>instinctively</strong></em> would have lent towards <strong>404 Not Found</strong> or <strong>410 Gone</strong> even if this wasn't a cache thing, as the cause of the failure was literally a missing/unavailable resource. However as per the spec <a href="https://www.rfc-editor.org/rfc/rfc7231" rel="nofollow noreferrer">RFC7231</a>:</p>
  <blockquote>
  <p>6.5.4.  404 Not Found</p>
  <p>The 404 (Not Found) status code indicates that the origin server
  did    not find a current representation <strong>for the target resource</strong> or is
  not    willing to disclose that one exists.  A 404 status code does
  not    indicate whether this lack of representation is temporary or<br />
  permanent; the 410 (Gone) status code is preferred over 404 if the<br />
  origin server knows, presumably through some configurable means, that
  the condition is likely to be permanent.</p>
  </blockquote>
  <p>or</p>
  <blockquote>
  <p>6.5.9.  410 Gone</p>
  <p>The 410 (Gone) status code indicates that access to <strong>the target<br />
  resource</strong> is no longer available at the origin server and that this<br />
  condition is likely to be permanent.</p>
  </blockquote>
  <p>These would appear to give the lie to that instinct.</p>
  <p>Does anyone have any experience in dealing with this, or similar, scenarios and good approaches? I feel I should go with what feels right and what I'd expect to see if consuming this service, rather than the letter of the text.</p>