0

While calling an apiController method from Postman I encountered a problem.

the method with params mentioned in Postman client call below method

    [AllowAnonymous]
    [HttpPost]
    public IActionResult Register([FromBody]UserDto userDto)
    {
        // map dto to entity
        //var user = _mapper.Map<User>(userDto);
        return Ok(new
        {
            userDto
        });

        //try
        //{
        //    // save 
        //    _userService.Create(user, userDto.Password);
        //    return Ok();
        //}
        //catch (AppException ex)
        //{
        //    // return error message if there was an exception
        //    return BadRequest(new { message = ex.Message });
        //}
    }

which maps the UserDto..

public class UserDto
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

but instead I get the error below :

Postman Client

Rodney S. Foley
  • 10,190
  • 12
  • 48
  • 66
Jason
  • 325
  • 2
  • 4
  • 12
  • 2
    FromBody is for JSON data. You are passing Form data. Those are not compatible. Use Body > Raw in Postman and put the JSON there. Otherwise, use a more proper tool like Swagger – Camilo Terevinto Oct 09 '18 at 17:30
  • FYI ... This is really a Postman question. – Rodney S. Foley Oct 09 '18 at 17:33
  • You can also use `[FromForm]` instead of FromBody if you have to use form data encoding. I believe it will also infer this if you skip the attribute altogether. – Eric Damtoft Oct 09 '18 at 17:36
  • @CamiloTerevinto: Did that too [{ "Id": 1, "FirstName": "Jason", "LastName": "Bond", "Username": "Jason", "Password": "helllo"}].. same result – Jason Oct 09 '18 at 17:39
  • 2
    @Jason That's because your method is set to receive a single entity, whereas you just tried passing an array of entities. –  Oct 09 '18 at 17:41
  • For the temp code used above after mentioning correct JSON type it returned as expected but for actual code `_userService.Create(user, userDto.Password); return Ok();` an unexpected exception at console appears.. `*info: Microsoft.AspNetCore.Server.Kestrel[32] Connection id "0HLHE2K4LHFNC", Request id "0HLHE2K4LHFNC:00000001": the application completed without reading the entire request body.* ` – Jason Oct 09 '18 at 18:56
  • Try this https://stackoverflow.com/questions/51424273/net-core-2-1-post-an-iformfile-using-postman-the-application-completed-with – Eldho Oct 10 '18 at 06:15

1 Answers1

0

Postman image

Use raw option and select json from dropdown and add your dto value in the body.

Raw

{
"FirstName" : "yourname",
"LastName": "lastname"
}
Eldho
  • 7,795
  • 5
  • 40
  • 77
  • That works just for the temp code I put in.. For actual code it seems some exception..`**info: Microsoft.AspNetCore.Server.Kestrel[32] Connection id "0HLHE2K4LHFNC", Request id "0HLHE2K4LHFNC:00000001": the application completed without reading the entire request body.**` – Jason Oct 09 '18 at 18:42