Getting a null value in the POST parameter

I get a null value always in the post post api rest request for the controller

In my controller

[HttpPost] public HttpResponseMessage PostCustomer([FromBody]Customer customer) { System.Diagnostics.Debug.WriteLine(customer); #CustomerApp.Models.Customer System.Diagnostics.Debug.WriteLine(customer.FirstName); #null } 

Model

 public class Customer { public int Id { get; set; } public string LastName { get; set; } public string FirstName { get; set; } } 

Request:

 POST: http://localhost:21894/api/customer/postcustomer Content-Type: application/json body: {FirstName: "xxxx", LastName: 'yyyy'} 

I tried the following solutions but nothing worked

https://myadventuresincoding.wordpress.com/2012/06/19/c-supporting-textplain-in-an-mvc-4-rc-web-api-application/

How to get POST data in WebAPI?

Can someone help me with the help or the correct link

Answer: I made a request for curling, instead of dealing with a postman, as if it gave me a solution

 $ curl -H "Content-Type: application/json" -X POST -d '{"FirstName":"Jefferson","LastName":"sampaul"}' http://localhost :21894/api/customer/postcustomer 
+5
source share
2 answers

I think you want to have the Customer object as input, as shown below, instead of string

 public HttpResponseMessage PostCustomer([FromBody]Customer customer) { 

Well do it below and try to double-check the request. He should work

 public HttpResponseMessage PostCustomer(Customer customer) { return OK(customer.FirstName); } 

Request:

 POST: http://localhost:21894/api/customer/postcustomer Content-Type: application/json; charset=utf-8 {"Id":101,"FirstName":"xxxx","LastName":'yyyy'} 
+5
source

set that your entity is a client, not a string

 [HttpPost] public Customer PostCustomer(Customer customer) { System.Diagnostics.Debug.WriteLine(customer); return customer; } 

Make sure the [HttpPost] attribute in your method is also not needed for [FromBody]

+3
source

All Articles