1

The problem is, when the method is entered, "session" is a null. As you can see, headers and JSON-object are correct. Could you tell me what I'm doing wrong?

My POST-request with angular:

        $scope.send = function () {
        var data = { "session": session };
        var req = {
            method: 'POST',
            data: data,
            headers: {'Content-Type': 'application/json'},
            url: '/api/myquiz/answers/'
        }

        $http(req).success(
            function () {
                quiz();
            });
    }

and C# code:

    [Route("answers")]
    [HttpPost]
    public IActionResult AnswerPost([FromBody] string session) {

       ...

    }

Chrome console:

Request URL:http://localhost:39634/api/myquiz/answers/
Request Headers
Provisional headers are shown
Accept:application/json, text/plain, */*
Content-Type:application/json
Origin:http://localhost:39634
Referer:http://localhost:39634/Team/Index
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36
Request Payload
{session: "92dfb7e432224702a553c98c294b29cf"}
session: "92dfb7e432224702a553c98c294b29cf"

3 Answers3

1

Per Web API documentation, try directly sending session (raw json string) instead of putting it in a dictinary (json object).

var req = {
    method: 'POST',
    data: '"' + session + '"',
    headers: {'Content-Type': 'application/json'},
    url: '/api/myquiz/answers/'
}
Rebornix
  • 5,272
  • 1
  • 23
  • 26
  • thanx a lot, this worked! But it works if you have only one parameter. According to this way you can try to convert a JSON-object: something like `data: "\'" + data + "\'"`. Then you get [object Object] in your WebAPI method (so amaizing). – shevchenko.ch Jun 02 '15 at 08:24
  • @shevchenko.ch Yes this only works when you have one parameter because you can only retrieve one basic type value from form body. It would be great if you can give it a upvote. – Rebornix Jun 02 '15 at 08:55
  • I would do it, but have not enough reputation( – shevchenko.ch Jun 02 '15 at 15:19
  • This solution helped me. When your api controller accepts one string parameter, warping it with `"` will ensure it work. But this only work for one string parameter scenario. – Hao Jun 25 '16 at 12:09
1

You are posting an object to WebApi:

var data = { "session": session };

With this you are saying that you are passing an object that has a session property on it. What you are telling your WebApi method to expect though is a string:

public IActionResult AnswerPost([FromBody] string session)

not an object. I think if you change your code to this:

$scope.send = function () {
        //var data = { "session": session };
        var req = {
            method: 'POST',
            data: session,
            headers: {'Content-Type': 'application/json'},
            url: '/api/myquiz/answers/'
        }

        $http(req).success(
            function () {
                quiz();
            });
    }

It should work. Hope this helps. If you are going to pass more params to the function in the future though, I would create a C# object to pass to the controller. like this:

public class AnswerPost
{
  public string Session {get; set;}
}

and then update the controller method to look like this:

[Route("answers")]
    [HttpPost]
    public IActionResult AnswerPost([FromBody] AnswerPost answerpost) {
...
}
peinearydevelopment
  • 11,042
  • 5
  • 48
  • 76
1

Web API actions require a value matching the parameter type to be able to deserialize the body.

So, if your Web API parameter is a value type, like your string session, you have to post a single value, i.e. something like 'my string', without extra wrappers.

If your Web API parameter was a complex one, for example a class with properties, then you would have to post a JSON object, like you do in your question.

I.e. your parameter should be an object of a class like this:

public class ParamClass
{
   public string session { get; set; } // Note: it's case.sensitive
}
JotaBe
  • 38,030
  • 8
  • 98
  • 117