Say I've got a resource
/Products/123
And each Product
has an associated Supplier
entity in the back end database. POST and PUT requests must specify a supplier ID, which is then used to fetch a Supplier entity from the database.
What should be returned if a user issues a PUT /Products/123
, which is found, but includes a bad Supplier ID, which is not?
404 Not Found
with a message specifying which resource wasn't found?
409 Conflict
?
The
404
status code may not be right choice because the resource that has not been found is not the target of your request:The
409
status code might be suitable for this situation, but is not be the best choice (I wouldn't define this situation as a conflict):I would go for
422
status code with a clear description in the response payload:The following diagram (extracted from this page) is pretty insightful when it comes to picking the most suitable
4xx
status code:Hello I would use the 404 as mentioned prior:
6.5.4. 404 Not Found
Because the product that you are looking for exists, but the Supplier ID not, so basically is like we are looking for you in a different city, you exist but not in that city, so we will say, hey we did not found you.
I believe that supplier and product they have a relationship and it is a hard relationship, that a product can not exist if you don't have a supplier for that product, so that means you can not update a product if you don't know it is supplier.
I don't believe that there is a correct answer for this question (unless some REST purist can shed some light) but we currently use (or abuse...)
HTTP 400
(Bad Request) with an additional HTTP Header explaining the error (i.e. X-Error: Invalid supplier ID). However a HTTP 422 would also be a good alternative. Statuses 404 or 409 would be confusing since there is no clear way to specify that the response is about a sub-resource.