82342

post data using asp.net Web API

Question:

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

<blockquote>

{"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)"}

</blockquote>

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

Answer1:

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

Body needs to set to JSON.

<a href="https://i.stack.imgur.com/j03y2.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/j03y2.png" data-original="https://i.stack.imgur.com/j03y2.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer2:

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

Recommend

  • WebApi help and multipe routes
  • Running a STA (Single-Threaded Apartment) thread from a Web API (2.1) controller
  • Self hosting Web API with OWIN on EC2
  • WCF Rest Error Handling
  • PayPal API adaptive pay Authentication failed. API credentials are incorrect
  • .NET Web Api - 404 - File or directory not found
  • Access bindingsource column value
  • Convert a NameValueCollection to a dynamic object [closed]
  • How can I detect if a URL is redirected to another one?
  • NRefactory: How do I access unresolved Named Arguments on a Property Attribute?
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • Zoom in and out of jPanel
  • Android Google Maps API v2 start navigation
  • Not able to aggregate on nested fields in elasticsearch
  • Firefox Extension - Monitor refresh and change of tab
  • System.InvalidCastException: Specified cast is not valid
  • Saving Changes After In-App Purchase Has Been Purchased
  • Webgrid not refreshing after delete MVC
  • Content-Length header not returned from Pylons response
  • Jquery UI tool tip close icon
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • Breeze - Deleted Items nav properties bug
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • To display the title for the current loaction in map in iphone
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • AT Commands to Send SMS not working in Windows 8.1
  • Traverse Array and Display in markup
  • SetUp method failed while running tests from teamcity
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Free memory of cv::Mat loaded using FileStorage API
  • How to stop GridView from loading again when I press back button?
  • Qt: Run a script BEFORE make
  • apache spark aggregate function using min value
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • Sorting a 2D array using the second column C++