locked
WEB API With EntityFramework - Post multiple parameters RRS feed

  • Question

  • User96565748 posted

    Hi, 

    I need a help.

    My Project has the following structure:

    • Project.Domain (Models: Person)
    • Project.Data(Mapping Person and DataContext)
    • Project.API(Controllers, etc)

    In my class Person:

     public class Person
     {
            public int Id { get; set; }
    
            public int IdReg { get; set; }
    
            public byte[] Image { get; set; }
    
            public DateTime DateReg { get; set; }
      }

    So, in my PersonController, i have the following  method:

     [HttpPost]
            [Route("people")]
            public HttpResponseMessage PostPerson(Person person)
            {
    
                if (person == null)
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
    
                try
                {
    
                    db.People.Add(person);
    
                    db.SaveChanges();
    
                    var result = person;
                    return Request.CreateResponse(HttpStatusCode.OK, result);
    
                }
                catch (Exception)
                {
                    return Request.CreateResponse(HttpStatusCode.InternalServerError, "Failed to insert person");
                }
    
            }

    But, I using PostMan to test my Api, and passing the parameters, i get some error, cause i dont know how to pass or get the image!

    {
    "Message": "The request entity's media type 'multipart/form-data' is not supported for this resource.",
    "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'Person' from content with media type 'multipart/form-data'.",
    "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
    "StackTrace": " em System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n em System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
    }

    Any help?

    Friday, July 14, 2017 3:02 PM

All replies

  • User1120430333 posted

    As you probably know, WebAPI is a Json based solution. Somehow within the DTO, the Image is being detected as a 'multipart/form-data' content-type.

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type

    https://www.linkedin.com/pulse/how-transfer-binary-files-pdfimages-etc-json-rahul-budholiya

    Friday, July 14, 2017 7:15 PM
  • User96565748 posted

    Yes, and in the Postaman, the Header Content-Type is setted with multipart/form-data, although it was not the solution.

    I found solutions to upload image, but with only one call at API and only one Image Parameter.

    I need to upload image as parameter of one object (person), because i have to store this object at Database.

    Friday, July 14, 2017 7:57 PM
  • User1120430333 posted

    Yes, and in the Postaman, the Header Content-Type is setted with multipart/form-data, although it was not the solution.

    I found solutions to upload image, but with only one call at API and only one Image Parameter.

    I need to upload image as parameter of one object (person), because i have to store this object at Database.

    Yeah, you can do that with no problem if this Postman was not involved I guess. The purpose of a object such as the Person object is to implement Encapsulation for an object's data, which is  hide the data within the object where data cannot be seen by anything outside of the object. So technically, the public property for Image should work within the Person object.

    Technically, the Person object should be called DtoPerson or Persondto, since that's the role it is playing.

    https://en.wikipedia.org/wiki/Data_transfer_object

    BTW about this.

    I need to upload image as parameter of one object (person), because i have to store this object at Database

    An object has properties not parameters..

    Saturday, July 15, 2017 12:30 AM
  • User-474980206 posted

    you need to use multi-part form. see:

       https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-2

    this will work from postman, see:

      https://www.getpostman.com/docs/postman/sending_api_requests/requests

    (multipart section). 

    note: be sure to set the content type correctly

    Sunday, July 16, 2017 10:40 PM