1
votes

Hi every one I am new to asp.net web API.I want to save user data in database and I am passing data using postman.

In my API controller I wrote

public HttpResponseMessage PostCustomer([FromBody] NewUser userData)
{
    userData = repository.Add(userData);
    var response = Request.CreateResponse<NewUser>(HttpStatusCode.Created, userData);
    string uri = Url.Link("DefaultApi", new { customerID = userData.ID });
    response.Headers.Location = new Uri(uri);
    return response;
}

NewUser is the class as followes

public class NewUser
    {
        [Key]
        public long ID { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public bool Status { get; set; }
        public DateTime? CreatedDate { get; set; }
        public DateTime? ModifiedDate { get; set; }
    }

In postman I form-data I passed key and value it return's me message as follows

{"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 'NewUser' from content with media type 'multipart/form-data'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"}

Please help me to save data from postman form-data.

2
Whats content type your using while checking from POSTMAN?Mithun Pattankar
I uses application/json; charset=utf-8Sabyasachi Mishra

2 Answers

6
votes

Try to follow the screen shot from POSTMAN, this might help you.

Body needs to set to JSON.

enter image description here

0
votes

if content type is set to application/json then your body should be structured in json format