5

Here is my client request code:

import request from 'axios';

//...

     let url = 'Login/SignIn',
                headers = {
                    headers: {
                        'Content-Type': 'application/json'
                    }
                },
                data = JSON.stringify( {
                    name: 'danil',
                    password: 'pwd'
                });

     request.post(url, data, headers);

looks fine by the first glance.

Request is pending here: enter image description here

But it all ended up like that in my controller:

enter image description here

Here's the code btw:

[HttpPost]
        public async Task<ActionResult> SignIn([FromBody]string name, [FromBody]string password)
        {

            var userLoginCommand = new UserLogInCommand {
                Login = name,
                Password = password
            };

            await _dispatcher.DispatchAsync(userLoginCommand);
            return Content(userLoginCommand.Result, "application/json");
        }

Whats wrong with it? What did I forgot?

I tried to play around with JSON.stringify by adding it and removing, tried to not sending headers (and then it throws 415 error) but no changes there. Still got nulls.

UPD: As Ali suggested in comments, passing data is goes fine if we use LoginModel for this like that:

 public class LoginModel
        {
            public string name { get; set; }
            public string password { get; set; }
        }

enter image description here

But why it's not going work just like that in a simple way?

6
  • Have you tried to create class with name and password properties and pass it like the following Task<ActionResult> SignIn([FromBody]LoginModel model)? Commented Aug 18, 2017 at 11:38
  • Nope, it should work like that as well isn't it? DTO is an option yep, but still Commented Aug 18, 2017 at 11:39
  • @Ali created and it works like that, with model but Commented Aug 18, 2017 at 12:01
  • Please read first note in section Binding formatted data from the request body Model Binding Commented Aug 18, 2017 at 12:08
  • 1
    why isnt working in the simple way?? Commented Sep 14, 2017 at 11:17

1 Answer 1

1

Only one parameter is allowed to read from the message body. In your example you have two parameters with FromBody attribute, that's why you have null values.

Please find documentation here: https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

"At most one parameter is allowed to read from the message body."

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.