Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode Email to pass to Web.API

I am needing to send an email address to a Web.Api get method. I need to check to see if the email address exists in our system before allowing someone to sign up for a new account.

My first thought was to encode the email address and then decode it once it was inside the Get Action

The URL looks something like this http://mysite/Api/RTSCredit/jharding%40email.com

However, This always errors with a 404 response

Here is the WebApiConfig.cs

 config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "api/{controller}/{emailAddress}",
      defaults: new { id = RouteParameter.Optional }
  );

When I send in a simple string http://mysite/Api/RTSCredit/someemail

It hits the Get action like I would prefer, but obviously can't return a useful value.

Here is the Get Action

[HttpGet]
public HttpResponseMessage Get([FromUri] string emailAddress)
{
    using (IRtsCreditDal rtsCreditDal = new RtsCreditDal())
    {
        var listOfExistingUsers = rtsCreditDal.GetUsersByEmail(emailAddress);
        if (listOfExistingUsers == null || listOfExistingUsers.Count == 0)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound, "not found");
        }
        else
        {
            return Request.CreateResponse(HttpStatusCode.OK); 
        }
    }
  }

What adjustments do I need to make in order to pass an email to this Action?

As I've been playing further, it doesn't seem to like the period in the URL. I could easily do something to replace that character but it feels like a hack.

like image 535
Jon Harding Avatar asked Dec 05 '13 15:12

Jon Harding


2 Answers

Try adding a trailing / at the end of the email reaching from the client:

http://localhost:8800/api/users/[email protected]/
like image 121
MichaelLo Avatar answered Oct 02 '22 05:10

MichaelLo


Change your default route back to

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

Remove the [FromUri] attribute from the Action, then you should be able to call your action using the url suggested by Maess:

/mysite/Api/RTSCredit?emailAddress=jharding%40email.com
like image 23
Jon Susiak Avatar answered Oct 02 '22 07:10

Jon Susiak