Web-development – How to organize methods that check username/email availability in a REST API

apirestweb-development

I am developing a RESTful API in my project, but I do not have experience on that. To fill these gaps I've been watching some videos, mainly from Apigee, which are great.
One situation brings a lot of discussion on my project and I hope you can help me clear it out. The client using the API is able to create a user by posting to my /Users url, but it has some requirements as: name format, username availability, email availability, valid region and many others.
I can easily validate the name format in my client app, that would be easy. The problem is when it comes to availability (mainly if the email and the user name are available to be user or are already taken).
We have two different lines of thoughts on my team:

the first one would break each verification in a different url, something like
/Users/EmailAvailable and /Users/UsernameAvailable
ou
/Available/Email and /Available/Username

They claim that it would make the interface cleaner (more intuitive) and would also keep cohesion, since one method has only one responsibility.

The second line of thought is to have only one URL
/User/Verify
and it will verify either the username, email or both, depending on the JSON it receives. They claim that it would only require one trip to the server and that cohesion is not a problem because, since the URLs are not necessarily mapped to methods, they can divide the calls into methods that do one thing each.
They also claim that the interface shouldn't care about cohesion

My questions:
1- Are these arguments about cohesion from the second line of though valid?
2- How would you do? (Either one of those or any other that you know of)

thanks!

Best Answer

  1. You can't validate and create something atomically with several Rest Api requests. But if you must really do it, a HEAD request to /users/?[email=user@example.com|user=aUsername] should return 404 if it's not already exists. That way you don't put adjective (available) or verb (verify) in the Url. Keep it simple.
  2. I would POST User to /users and if username or email had been used, return 409 (conflict) with explanation either username or email is already exists.
Related Topic