What's the best/restful way to design an API endpoint for checking existence of resources?
For example there is a user database. While new user tries to sign up I want to check if email has been used on-the-fly.
My idea is: POST /user/exists
and payload would be something like {"email": "[email protected]"}
. The response would be either 200 OK or 409 Conflict.
Is this a proper way?
Thanks!
HEAD
is the most effecient for existence checks:
HEAD /users/{username}
Request a user's path, and return a 200
if they exist, or a 404
if they don't.
Mind you, you probably don't want to be exposing endpoints that check email addresses. It opens a security and privacy hole. Usernames that are already publicly displayed around a site, like on reddit, could be ok.