0

I'm using angularjs and I'm trying to make a HttpPost call to my web api.

My api method:

[HttpPost]
    [Route("authentication/getkey")]
    public IHttpActionResult GetKey([FromBody]string password) {
        //Do stuff
    }

my call:

service.getKey = function (password) {
            return $http.post('api/authentication/getkey', JSON.stringify(password))
                .then(function(result) {
                    return result.data;
                });
        }

Now this works fine, but do I really need to use JSON.stringify? I tried sending it like below, but all of them get password = null. Do I have to use JSON.stringify or am I doing it wrong in my other examples?

//Doesnt work
service.getKey = function (password) {
    return $http.post('api/authentication/getkey', password)
        .then(function(result) {
             return result.data;
         });
    }

//Doesnt work
service.getKey = function (password) {
    return $http.post('api/authentication/getkey', {password})
        .then(function(result) {
             return result.data;
         });
    }
2
  • You will need to send the data in your POST request as JSON format because by default angular $http service sends the Content-Type as application/json. The JSON.stringify will convert your data to to a JavaScript Object Notation (JSON) string. Is there a reason you don't want to use JSON.stringify? One way would be to change the Content-Type. I can provide some sample code if you want. Commented Apr 5, 2016 at 13:08
  • @VivekN please do that. I just wanna see other examples of how to do it - or how I should do it Commented Apr 5, 2016 at 13:19

2 Answers 2

2

If you don't want to use JSON.stringify, the other option will be to send the data as application/x-www-form-urlencoded as pointed in other answer as well. This way you are sending the data as form data. I'm not sure about the syntax of the $http.post Shortcut method but the idea is the same.

service.getKey = function (password) {
    $http({
            method: 'POST',
            url: 'api/authentication/getkey',
            data: $.param({ '': password }),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        })
 .then(function(result) {
             return result.data;
         });
Sign up to request clarification or add additional context in comments.

Comments

2

From Microsoft's Web API official documentation about Parameter Binding in ASP.NET Web API:

When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object).

Angular $http service sends Content-Type: application/json as header by default in POST requests, as you can see from the official docs, so Web API is trying to bind your request body using his JsonFormatter. Because of this you have to provide him a well formatted Json string (not a Json Object with a string inside) to correctly bind his raw string parameter.

As a side note, you could also send a request using application/x-www-form-urlencoded as Content-Type header, but then you will have to format your body as form parameters (using something similar to jQuery $.param( .. ))

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.