When building a RESTful API and a user provides an id of resource that does not exist, should you return 404 Not Found
or 400 Bad Request
.
For example:
https://api.domain.com/v1/resource/foobar
Where foobar does not exist.
Based on the code above, is it correct to return a NOT_FOUND status ( 404 ), or should I be returning 204 , or some other more appropriate code? If you would expect a resource to be there, because it is looked up using an ID, then a 404 is expected. Something goes wrong, the resource you need to be there is not found.
If the server does not know, or has no facility to determine, whether or not the condition is permanent, the status code 404 (Not Found) SHOULD be used instead.
It doesn't matter if the resource returns an ID field. It is up to the client to keep a map of where it was obtained from. In a + b, the server would create a new resource.
You fix this by opening the listen step in your VSM file, and changing the base path in there, so you don't get a 404 error. You could change that to "/api/" so any api requests are dealt-with, or "/api/retrieveId/" so only retrieveId messages are dealt-with, or "/" so all requests are dealt-with.
I would return 404 in case of resource does not exist(means the url path is wrong) and i will return 400 only if the rest call is made with some invalid data (@PathParam) for example
https://api.domain.com/v1/profile/test@email : here i am trying to get profile of email id, but the email itself is wrong, so I will return 400.
https://api.domain.com/v1/profile1111/[email protected] will return 404 because the url path is invalid.
Should be 404 ( Not Found ). 400 is used if you can't fulfill the request due to bad syntax, however for your case, the syntax is correct, however there is no resource foobar.
You can use 400 if user uses non-existent API like below :
https://api.domain.com/v1/nonexistAPI/xyz/xyz
You can also refer to this REST API Design Blog which tell you how to design your REST error codes.
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