1

IN angular js i am trying to send a post request her is the controller. .controller('ActivationController',['$http','$location','$routeParams','AuthService',

function($http, $location, $routeParams, AuthService){

        var location = $location.path();
        var activation_code = $routeParams.code;
        var activationLink = "http://localhost:18678/api/User/ActivateUser";
        console.log(activation_code);
        if(activation_code){
            $http({method:"post", url:activationLink, data:activation_code}).success(function(response){
                console.log(response);
            }).error(function(error){
                console.log(error);
            });
        }

    }]);

and in asp.net web API her is the method.

 [HttpPost]
        public HttpResponseMessage ActivateUser([FromBody]string activation_code)
        {
            if (!string.IsNullOrWhiteSpace(activation_code))
            {
                string decode_token = HttpUtility.UrlDecode(activation_code); ;
                string token_string = Crypto.Decrypt(activation_code, passPhrase);

                if (token_string != null)
                {
                    User activateAcc = db.Users.Where(user => user.ConfirmToken == token_string).SingleOrDefault();
                    if (activateAcc != null)
                    {
                        activateAcc.IsActive = true;

                        try
                        {
                            db.SaveChanges();
                            var credential = new UserCredential(); 
                            credential.EmailAddress = activateAcc.UserMail; 
                            credential.Password = activateAcc.UserPassword;

                            return Request.CreateResponse(HttpStatusCode.OK, credential);
                        }
                        catch
                        {
                            return Request.CreateResponse(HttpStatusCode.Ambiguous, "cannot confirm account");
                        }
                    }
                    else
                    {
                        return Request.CreateResponse(HttpStatusCode.NotAcceptable, "invalid account");
                    }
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.NoContent, "invalid token data");
                }
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.NoContent, "missing activation code");
            }
        }

The problem is when the controller fire the request is made but without sending any data to the server. The [FromBody]string activation_codeis null

1
  • 1
    Not sure, but I think your data in the request should be another JS object: $http({method:"post", url:activationLink, data: {activation_code:activation_code}}).success(function(response){.. Commented Aug 4, 2014 at 8:15

1 Answer 1

3

Wrap your parameter in quotes:

$http({method:"post", url:activationLink, data: '"' + activation_code + '"'});

Explanation

For Web API to bind to a simple string primitive, the body must be specified as:

"some string here"

For example:

POST http://localhost:5076/api/values HTTP/1.1
 User-Agent: Fiddler
 Host: localhost:5076
 Content-Type: application/json
 Content-Length: 7

 "Alice"

The quotes are important. For more information, check this article.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.