5

I receive a null value always in web api rest post request to a 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 works out

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 anybody guide me with help or the correct link

Answer: Made a curl request instead of dealing with postman as like this gave me the solution

$ curl -H "Content-Type: application/json" -X POST -d '{"FirstName":"Jefferson","LastName":"sampaul"}' http://localhost
:21894/api/customer/postcustomer
Community
  • 1
  • 1
Sam
  • 5,040
  • 12
  • 43
  • 95

2 Answers2

5

I think you mean to have a Customer object as input like below instead of string

public HttpResponseMessage PostCustomer([FromBody]Customer customer)
 {

Well make this below change and try reposting the request. It 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'}
Rahul
  • 76,197
  • 13
  • 71
  • 125
3

set your entity to be a customer not a string

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

Make sure you have [HttpPost] attribute on your method also no need for the [FromBody]

johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • System.Diagnostics.Debug.WriteLine(customer.FirstName); does not print any value – Sam May 17 '17 at 13:59
  • @Jefferson what version of Web-Api are you using? Are you posting from AJax or a form? – johnny 5 May 17 '17 at 14:02
  • Just so you know the post you're following is to support plain/text not JSON if you're just trying to get you customer model binded, You should probably follow a different tutorial. I will post some trouble shooting update in a second – johnny 5 May 17 '17 at 14:05
  • System.Diagnostics.Debug.WriteLine(customer); prints CustomerApp.Models.Customer – Sam May 17 '17 at 14:05
  • It should because it will always be that type you should just try printing the first name – johnny 5 May 17 '17 at 14:06
  • I am not getting any o/p – Sam May 17 '17 at 14:10
  • Do you have HttpPost attribute above your method? – johnny 5 May 17 '17 at 14:11
  • I noticed that you are following a post to support plain/text this could be causing issues in your configuration. Start a new project with only the customer and controller. – johnny 5 May 17 '17 at 14:25
  • http://dotnetmentors.com/web-api/rest-based-crud-operations-with-asp-net-web-api.aspx this is the tutorial I followed – Sam May 17 '17 at 14:28
  • @Jefferson, if you followed that tutorial you should have everything there step by step, why aren't you returning a customer. Try like how I have it above now – johnny 5 May 17 '17 at 14:31
  • I think there is a problem with retrieveing the data. There is something to do with json parse – Sam May 17 '17 at 16:49
  • The json parameter had to be sent with a string. This cleared the problem. – Sam May 18 '17 at 05:32