1

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? Commented Apr 17, 2016 at 12:19
  • I uses application/json; charset=utf-8 Commented Apr 17, 2016 at 12:20

2 Answers 2

6

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

Body needs to set to JSON.

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

In terms of Web API it is not mandatory. But if your server side logic requires some of them you should make sure to decorate them with the proper validation attributes to ensure that they are present.
POSTMAN acts client to interact with Web API. Header's Content Type, Body that sends JSON request. This JSON will be serialized to object to be used further.
Thanks this worked. I mistake was I was using form-data. How can I do by using form-data?
Please accept the answer :) .. I usually don't use form data coz Web APIs should be consumed by different clients. Plz go thru this link asp.net/web-api/overview/advanced/sending-html-form-data-part-1
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.