title: Is HTTP 404 an appropriate response for a PUT operation where some linked resource is not found?
body: <p>Imagine a REST webservice where you are adding items to some type of container.  For example, lets say we could add a attendee #789 to a event #456 like this:</p>
  
  <pre><code>PUT http://..../api/v1/events/456/attendees/789
  </code></pre>
  
  <p>If either event #456 or attendee #789 do not exist, is it correct to return an HTTP 404 (along with a detailed error payload explaining what the problem was, e.g. <code>{ "error" : { "message" : "Event 456 does not exist" , "code" : "404" } }</code>?</p>
  
  <p>Similarly, what if I am creating something new which refers to another object, but the other object does not exist?   For example, imagine I am creating an event at Location #123</p>
  
  <pre><code>PUT http://..../api/v1/event
  { "location": 123, "name": "Party", "date": "2012-05-23", ...etc... }
  </code></pre>
  
  <p>If Location #123 does not exist, is it also correct to return 404 (along with detail in response)?  If not, what would be appropriate -- just a 400?</p>
  
  <p>According to the HTTP 1.1 spec <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html">http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html</a></p>
  
  <blockquote>
    <p><strong>9.6 PUT</strong> ... If the resource could not be created or modified with the Request-URI, an appropriate error response SHOULD be given that reflects the nature of the problem</p>
  </blockquote>
  
  <p>So that would seem to be a good vote for responding with 404.  <em>However</em> for some reason I can't quite put my finger on, responding to PUT (or POST) with a 404 seems odd to me... Perhaps it's because 404 means that the resource cannot be found, but in this case our resource is actually a linkage between two other resources, and it's one of those two resources that can't be found.</p>
  
  <p>Don't get too worried about my exact examples here -- they are made up to illustrate the point.  The main question is: is 404 an appropriate response to a PUT operation that fails because a linked resource is not found?</p>
  
  <p>It would be excellent if you can point to references -- I'm having a hard time finding any that get into this level of detail and are also sufficiently credible.  Especially as regards treatment of resource relationships in REST API design.</p>
  
  <p><strong>Updated thinking</strong> I'm thinking that possibly the first example should return 404, the second should not.  The reasoning being that in the first case the resource we're adding uses event 456 and attendee 789 as it's composite primary key; the second case location is only a foreign key.  In the second case an error should be returned, but not a 404 -- possibly a 412 Precondition Failed or maybe just a 400 Bad Request.  Thoughts?  </p>