- Status code
400 Bad Request
is used when the client has sent a request that cannot be processed due to being malformed. - Status code
404 Not Found
is used when the requested resource does not exist / cannot be found.
My question is, when a client sends a request to an endpoint my API does not serve, which of these status codes is more appropriate?
Should an endpoint be considered a "resource", and thus a 404
be returned? My issue with this is that if the client only checks the status code, they cannot tell the difference between a 404
indicating that they got to the correct endpoint, but there was no result matching their query, versus a 404
indicating that they queried a non-existing endpoint.
Alternatively, should we expect that a client has prior knowledge of all available API endpoints, and thus treat their request as malformed and return a 400
if the endpoint they are trying to reach does not exist?
Maybe this depends on whether the endpoints are REST or not. If they are REST endpoints, the client should not need prior API knowledge, but be able to learn about all relevant API endpoints by navigating the API from a single root endpoint. In such a case, I guess 404
would be more appropriate.
In my specific case right now, this is an internal (non-REST) HTTP API, where I expect the client to have prior knowledge of all API endpoints, so I am leaning towards 400
, to avoid issues where 404
from accessing the wrong endpoint could be misconstrued as a 404
indicating that what they sought from the correct endpoint could not be found.
Thoughts?