0

I come to post here after reading a lot of topics about the same mistake I have now.

I have a WEB API that works in GET but not in POST. When I send him JSON with POSTMAN, I get an error message in the ModelState which is ModelError ModelBinding

This is my model class:

[DataContract]
public class Employee
{
        [DataMember]
        public int ID { get; set; }
        [DataMember(IsRequired = true)]
        public string FirstName { get; set; }
        [DataMember(IsRequired = true)]
        public string LastName { get; set; }
}

Here is the Post method of the controller of my API :

public IHttpActionResult Post([FromBody]Employee emp)
{
            if (!ModelState.IsValid)
                return BadRequest("Invalid data");

            ...
            return Ok();
}

In this controller, I pass in my if and it returns "invalid data" to me

I tried everything, I removed the[DataContract] in my model, but nothing works.

Here is an example of the JSON I am sending:

{
    "FirstName" : "John",
    "LastName" : "Malon"
}

My header contains the contentType JSON

Full code from Postman :

POST /api/employees HTTP/1.1
Host: localhost:49463
Content-Type: application/json
cache-control: no-cache
Postman-Token: 5ac38b20-425a-4ac7-995a-323fbd0bb9a5
{
    "FirstName" : "John",
    "LastName" : "Malon",
}------WebKitFormBoundary7MA4YWxkTrZu0gW--

EDIT : It seems that the emp object that I receive in the Post method is null I saw this post : Post parameter is always null But english is not my primary language and I cant figure out what is the solution with the = I understand that I need to put an = in the JSON that I send, is that right ?

Thank you in advance for your help!

  • Perhaps is trying to validate ID, try sending ID:0 or something – The One Feb 27 '19 at 18:32
  • Precisely the ID is automatically incremented with the DB, I don't want to transmit it in JSON –  Feb 27 '19 at 18:34
  • But can you just test it with Postman to check if that's the issue? – The One Feb 27 '19 at 18:36
  • 1) Have you debugged and validated what properties are set for the emp object. 2) Using post man have you tried setting header : ContentType >> application/json ? 3) When debugging the ModelState should tell you why its failing... – eVolve Feb 27 '19 at 18:37
  • @eVolve I put the contentType on Json and I just have the FirstName with the value {System.Web.Http.ModelBinding.ModelError} in the ModelState in debug mode –  Feb 27 '19 at 18:40
  • @Tuco Doesn't work with the ID in JSON –  Feb 27 '19 at 18:41
  • 1
    Get the errors from ModelState – The One Feb 27 '19 at 18:43
  • @Tuco in the _innerDictionnary in the ModelState I have this : {[emp.FirstName, System.Web.Http.ModelBinding.ModelState]} –  Feb 27 '19 at 18:46
  • Sorry if this sounds rude, but please search this web site or google: "WebAPI get Errors ModelState" – The One Feb 27 '19 at 18:47
  • @Tuco I said I did almost all topics of the internet so please –  Feb 27 '19 at 18:50
  • Ok, try this answer please https://stackoverflow.com/a/11686747/3596441 – The One Feb 27 '19 at 18:51
  • @itsdeft so to confirm, you are telling us that when you debug you can can see all the properties you sent down assigned in the emp object? The ModelState contains an Errors property that should have a string that will tell you why it is failing. You are yet to provide this. In postman you have selected Post as the type of request? – eVolve Feb 27 '19 at 18:56
  • @Tuco The list contains an empty string ... –  Feb 27 '19 at 18:57
  • @eVolve Yes I selected POST method in Postman and NO I see nothing my emp object is NULL –  Feb 27 '19 at 18:58
  • Well, can you please post in this post your post request from postmant? :) I mean the raw text, with headers and all – The One Feb 27 '19 at 18:59
  • @Tuco Ok I just added it ! :) –  Feb 27 '19 at 19:02
  • Nothing is wrong with what you are doing! – Amir Molaei Feb 27 '19 at 19:14
  • Can you please add [DataContract] attribute to your Employee class – The One Feb 27 '19 at 20:43
  • @Tuco Oops it didn't appear on the code I posted but I wrote it ! –  Feb 27 '19 at 20:58
  • @Tuco I updated my post I found that my object was null –  Feb 27 '19 at 22:39
  • can you share your solution to github or somewhere to check it? – Hien Nguyen Feb 28 '19 at 13:06

2 Answers2

0

Be sure you are sending the application/json header from postman. Otherwise I don't see any problems with the posted code.

ScottyD0nt
  • 348
  • 2
  • 8
0

You're defining a class that contains a non-nullable int but you are not providing one in your POST body. This will always fail validation unless you provide a value for that property.

If you want this to succeed you can either -

Option 1 - Change your model to accept a null Id:

public class Employee
{
        public int? ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
}

Option 2 - Provide a default (but valid!) int in your POST body that must be replaced by your database/backend code once an Id has been generated:

{
    "ID" : 0,
    "FirstName" : "John",
    "LastName" : "Malon"
}
Steve Land
  • 4,852
  • 2
  • 17
  • 36