Home > Software design >  Unsupported Media Type on postman with form-data
Unsupported Media Type on postman with form-data

Time:05-07

I am facing an issue when trying to post form-data on postman. It works when I use Raw JSON but not with form-data.I've created a web api using .net core and I am using Mongodb.

Here is the error :

enter image description here

Here is my controller (I'm on .Net core and using Mongodb as database) :

namespace CRUD_App.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : Controller
    {
        private readonly IConfiguration _configuration;
        private readonly IWebHostEnvironment _env;

        public EmployeeController(IConfiguration configuration, IWebHostEnvironment env)
        {
            this._configuration = configuration;
            this._env = env;
        }


        [HttpGet]
        public IActionResult Get()
        {
            MongoClient dbClient = new MongoClient(_configuration.GetConnectionString("EmployeeAppCon"));

            var dbList = dbClient.GetDatabase("BDD").GetCollection<Employee>("Employee").AsQueryable();

            return Json(dbList);
        }

        [HttpPost]

        public IActionResult Post(Employee emp)
        {
            MongoClient dbClient = new MongoClient(_configuration.GetConnectionString("EmployeeAppCon"));

            int LastEmployeeId = dbClient.GetDatabase("BDD").GetCollection<Employee>("Employee").AsQueryable().Count();

            emp.EmployeeId = LastEmployeeId   1;

            dbClient.GetDatabase("BDD").GetCollection<Employee>("Employee").InsertOne(emp);

            return Json("Employee Added Successfully");
        }

        [HttpPut]
        public IActionResult Put(Employee emp)
        {
            MongoClient dbClient = new MongoClient(_configuration.GetConnectionString("EmployeeAppCon"));

            var filter = Builders<Employee>.Filter.Eq("EmployeeId", emp.EmployeeId);

            var update = Builders<Employee>.Update.Set("EmployeeName", emp.EmployeeName)
                                                  .Set("Department", emp.Department)
                                                  .Set("DateOfJoining", emp.DateOfJoining)
                                                  .Set("PhotoFileName", emp.PhotoFileName);

            dbClient.GetDatabase("BDD").GetCollection<Employee>("Employee").UpdateOne(filter, update);

            return Json("Employee Updated Successfully");

        }

        [HttpDelete]
        public IActionResult Delete(int id)
        {
            MongoClient dbClient = new MongoClient(_configuration.GetConnectionString("EmployeeAppCon"));

            var filter = Builders<Employee>.Filter.Eq("EmployeeId", id);

            dbClient.GetDatabase("BDD").GetCollection<Employee>("Employee").DeleteOne(filter);

            return Json("Employee Deleted Successfully");
        }

Does it support form-data this way ?

CodePudding user response:

As you are using asp.net core then you can use the [FromForm] attribute

   public IActionResult Post([FromForm] Employee emp)

See the docs for more details.

For people using ASP WebApi then you have to read it from HttpContext.Current.Request.Form instead.

  • Related