question

PolachanPaily-2805 avatar image
1 Vote"
PolachanPaily-2805 asked RenaNi-MSFT answered

Status 415 Unsupported Media Type Form-Data

0


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"

 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; }
     }
 [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();
         }
 }

115633-image.png


dotnet-aspnet-core-webapi
image.png (35.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RenaNi-MSFT avatar image
3 Votes"
RenaNi-MSFT answered

Hi @PolachanPaily-2805,

You post form data to backend, so you need change FromBody to FromForm:

 public async Task<ActionResult> Post([FromForm] PersonCreationDTO personCreation)


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Best Regards,

Rena

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.