1

Below is my HttpPost method where i am trying to pass input as DTo's

[HttpPost("register")]
    public async Task<ActionResult<AppUser>> Register(RegisterDto registerDto)
    {
        if (await UserExists(registerDto.Username)) return BadRequest("Username is taken");

        using var hmac = new HMACSHA512();
        var user = new AppUser
        {
            UserName = registerDto.Username.ToLower(),
            //UserName = username,
            PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registerDto.Password)),
            //PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password)),
            PasswordSalt = hmac.Key
        };
        _context.Users.Add(user);
        await _context.SaveChangesAsync();

        return user;
    }

}

And here it is the Dto class which i have created and which i passed in tht HttpPost method

Register Dto Class:-

public class RegisterDto
{
    public string Username { get; set; }
    public string Password { get; set; }
}

When i m trying to test this method in postman , it is throwing error as 415 unsupported media type , check the image below. Postman error:-

enter image description here

Asif rocky
  • 21
  • 2

2 Answers2

1

SORRY MY BAD,

I need to select the content type as json in postman which was plain text before, so it was throwing error.

How to set content-type in postman as JSON (application/json).

Go to the body inside your POST request, there you will find the raw option.

Right next to it, there will be a drop down, select JSON (application.json).

Asif rocky
  • 21
  • 2
0

You might want to decorate your DTO with JSON properties so it looks like this:

public class RegisterDto
{
    [JsonProperty("username")]
    public string Username { get; set; }
    [JsonProperty("password")]
    public string Password { get; set; }
}

and then in your post JSON use matching casing for the data

{
   "username":"jane@doe.com",
   "password":"somethingsecure"
}
cminus
  • 1,163
  • 12
  • 25