Thomas.Benz
Thomas.Benz

Reputation: 8599

Null binding from POST request body

I have 2 asp.net web api actions in an ApiController-subclassed controller. The 2 action methods are POST. One method is working with non-null request body. But the other action method always get null request body. I do not know the reason why the second POST action does not work. Please help.

The following POST action method works OK with non-null request body binding: parameter "model" in method signature is bound correctly.

[HttpPost]
[Route("create/{sessionid:guid}")]
public HttpResponseMessage CreateAccount(Guid sessionid, UserAccountDto model)
{
    var result = new HttpResponseMessage(HttpStatusCode.OK);


    return Request.CreateResponse(HttpStatusCode.OK, model);
}

Dto class:

public class UserAccountDto
    {
        public long? AccountId { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Salt { get; set; }
        public string PlainTextPassword { get; set; }
    }

Call with API by using Advanced Rest Client tool plugged in Chrome

POST http://localhost:55540/guests/create/13DD8111-8A00-48CE-997F-28EE3C88895D

{ 
    email:"[email protected]",
    password:"fkdld",
    salt:"salt",
    plaintextpassword :"abc"
}

The 2nd ApiController POST action method does not work. Parameter "model" in method action is always null; it is not bound correctly for some reason.

[HttpPost]
    [Route("convert/{sessionid:guid}")]
    public IHttpActionResult ConvertAccount(Guid sessionid, LinkedAccountThinDto model)
    {
        var accountType = model.AccountType;
        var accountKey = model.AccountKey;

        return Ok();
    }

Custom Dto class:

public class LinkedAccountThinDto
{
    public string AccountType { get; set; }
    public string AccountKey { get; set; }
}

Call with API by using Advanced Rest Client tool plugged in Chrome

POST http://localhost:55540/guests/convert/13DD8111-8A00-48CE-997F-28EE3C88895D

{ 
        accounttype: “abcdf”,
        accountkey: “1234567”
} 

Upvotes: 1

Views: 818

Answers (1)

Thomas.Benz
Thomas.Benz

Reputation: 8599

As Stilgar said, I made a mistake when creating that JSON string for the 2nd Api action by using MS Word instead of the text editor of the Advanced Rest Client tool. That caused the problem of created quotes which were not understood by the tool. I re-created the JSON string directly inside the tool, and the problem is solved.

Thank you for all of you.

BTW, the following JSON strings all work fine with my second Api method regardless of the case of attribute keys and of used quotes:

{"AccountType": "abcdf","AccountKey": "1234567"} 
{"accounttype": "abcdf","accountkey": "1234567"} 
{accounttype: "abcdf",accountkey: "1234567"}

Upvotes: 0

Related Questions