Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to call Post method from API controller from PostMan using form-data from Body . But after click of the Send button from postman , it is showed an error message "Unsupported Media Type"

"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13", "title": "Unsupported Media Type", "status": 415, "traceId": "|cd267e9d-4f557653ef6391ef."

Here is the code. Model class

public class PersonCreationDTO
        [Required]
        [StringLength(120)]
        public string PersonName { get; set; }
        public string Biography { get; set; }
        public DateTime DateOfBirth { get; set; }
        public IFormFile Picture { get; set; }
public class Person
        public int Id { get; set; }
        [Required]
        [StringLength(120)]
        public string PersonName { get; set; }
        public string Biography { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string Picture { get; set; }

Controller

[ApiController]
    [Route("api/people")]
 public class PeopleController:ControllerBase
        private readonly ApplicationDbContext context;
        private readonly IMapper mapper;
        public PeopleController(ApplicationDbContext context, IMapper mapper)
            this.context = context;
            this.mapper = mapper;
    [HttpPost]
        public async Task<ActionResult> Post([FromBody] PersonCreationDTO personCreation)
            var person = mapper.Map<Person>(personCreation);
            context.Add(person);
            //await context.SaveChangesAsync();
            //var personDTO = mapper.Map<PersonDTO>(person);
            //return new CreatedAtRouteResult("getPerson", new { Id = personDTO.Id }, personDTO);
            return NoContent();
  • In Postman click headers and click (select) the first Content Type - multipart/form-data; boundary= calculated when request is sent

  • In your controller action replace [FromBody] with [FromForm]

    Add [FromForm] Instead of [FromBody]

    [HttpPost]
    public async Task<ActionResult> Post([FromForm] PersonCreationDTO personCreation)
      //clarify code
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •