What is the correct http error code if id indicated in body not exists?
Example: The cityId 123 does not exists in database
POST /profiles
{
"name":"foo",
"cityId":123
}
Is it 404? 400? or 422?
It's up to you in the end, but I'd say 422.
400 implies that the request is malformed, which isn't the case. The request is perfectly valid lexically, hence why you can parse and process it OK.
http://www.restpatterns.org/HTTP_Status_Codes/400_-_Bad_Request
404 implies that the URL requested does not exist. The resource (i.e. the web service endpoint) does exist. I might suggest a different answer if the id was part of the URL, i.e. in a query string. 404s refer to the URL only in my experience, not to POSTed bodies. Also note that the link below refers to 404s only in the context of the URL, not the contents of the POST body.
http://www.restpatterns.org/HTTP_Status_Codes/404_-_Not_Found
Therefore this leaves 422 as the only valid candidate.
http://www.restpatterns.org/HTTP_Status_Codes/422_-_Unprocessable_Entity
Another poster suggested 204, but this is incorrect because the request has been processed, but has not been successful. All 2** status codes imply some level of success.
http://www.restpatterns.org/HTTP_Status_Codes/204_-_No_Content
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With