What is the appropriate response for a broken link in the requested entity?

advertisements

Say I have a RESTful API that binds a user to a company:

PUT http://example.com/users/john.smith

{
  "company": "http://example.com/companies/Nintendo"
}

but the referenced company does not exist (possibly due to a race condition, possibly due to user error). The operation cannot complete successfully because the database requires foreign keys to point to existing rows. What is the appropriate response code and why?


I would return a HTTP 400 - Bad Request, probably adding in the response body some hint (e.g. Nintendo is not a valid option). It's not a 404 - Not found error, since the URL and the resources referenced in it are correct.

As a side note about the body of your request

{
  "company": "http://example.com/companies/Nintendo"
}

I would expect users to pass only a name, or an id about the company, not the whole URI. You add more complexity without possibly gaining anything. Imagine a user passing a valid company name, e.g. Sega, in a URI that has a typo, e.g. comppanies. I'm guessing your backend would try to access this URI, and, failing that, it would return an error. Well, you could, but to me it seems that you are making both yours and your users life difficult.