1

I am in the process of writing a WCF webapi application and have a need to check whether an email address is taken or not. This needs to be a query the client-side code can do before attempting a PUT.

So, what I'm trying to do is use HEAD in conjunction with HTTP status codes. I am a little unsure how to go about doing that as it's a simple yes/no response which is required. So, I've used HttpResponseExceptions to return the relevant status code.

    [WebInvoke(Method = "HEAD", UriTemplate = "{email}")]
    [RequireAuthorisation]
    public void IsEmailAddressTaken(string email)
    {
        if (!Regex.IsMatch(email, Regexes.EmailPattern))
        {
            throw new RestValidationFailureException("email", "invalid email address");
        }

        if (_repository.IsEmailAddressTaken(email))
        {
            throw new HttpResponseException(HttpStatusCode.OK);
        }

        throw new HttpResponseException(HttpStatusCode.NoContent);
    }

This just doesn't "smell" right to me.

am I going about doing this kind of yes/no operation the right way?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Antony Scott
  • 21,690
  • 12
  • 62
  • 94

2 Answers2

2

I think it would be ok to just return OK for "exists" and 404 for "does not exist"

Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124
  • The reason I wanted a "pre check" is so that the user can immediately see that the email is already taken without having to fill in the rest of the information on a registration page. So, we can go straight to the "reset password" page instead. I know I get annoyed when I go enter all my information only to be told "this email address has already been used". At which point I remember I already have an account! – Antony Scott Jan 17 '12 at 21:51
2

My suggestion is to return a HttpResponseMessage instead of throwing exceptions. Is your RestValidationFailureException being handled anywhere? If not, it will result in a 500 status code, which does not seem adequate.

Antony Scott
  • 21,690
  • 12
  • 62
  • 94
Pedro Felix
  • 1,056
  • 1
  • 8
  • 12
  • thanks. I'd just got there myself :) I think I was probably adding my solution to my question as your were typing this answer. – Antony Scott Jan 18 '12 at 12:12