Search code examples
c#restwebapiasp.net-apicontroller

Why the webapi's post method is receiving the parameter of type interface as null when called from a postman?


made this webapi.

[HttpPost]
    public void Post([FromBody] Models.IHero hero)
    {
        Models.Heroes heroes = new Models.Heroes();

        heroes.AddHeroes(hero);
    }

calling it from Postman.

https://localhost:44320/api/values?Id=1&Name=Shankar

but hero received in method returns null.

Interface:

 public interface IHero
    {
        int Id { get; set; }
        string Name { get; set; }
    }

Update:

Converted IHero to Hero class

   public class Hero
    {
        int Id { get; set; }
        string Name { get; set; }
    }

and used in the Post method.

 public void Post([FromBody] Models.Hero hero)
        {
            Models.Heroes heroes = new Models.Heroes();

            heroes.AddHeroes(hero);
        }

Solution

  • You have [FromBody] attribute, which means you need to pass data in the body of the Request, but not in Query params enter image description here