87

I'm still learning web API, so pardon me if my question sounds stupid.

I have this in my StudentController:

public HttpResponseMessage PostStudent([FromBody]Models.Student student)
{
    if (DBManager.createStudent(student) != null)
        return Request.CreateResponse(HttpStatusCode.Created, student);
    else
        return Request.CreateResponse(HttpStatusCode.BadRequest, student);
}

In order to test if this is working, I'm using Google Chrome's extension "Postman" to construct the HTTP POST request to test it out.

This is my raw POST request:

POST /api/Student HTTP/1.1
Host: localhost:1118
Content-Type: application/json
Cache-Control: no-cache

{"student": [{"name":"John Doe", "age":18, "country":"United States of America"}]}

student is supposed to be an object, but when I debug the application, the API receives the student object but the content is always null.

CarenRose
  • 1,266
  • 1
  • 12
  • 24
InnovativeDan
  • 1,067
  • 2
  • 8
  • 10
  • 3
    Just a side-note to anyone finding this later (as I just did) while looking at a similar issue: Web API should return JSON containing the exception (as if you'd caught the exception in your code) which can be used to diagnose the problem. Seems obvious, but I suspect I'm not the only person who hasn't thought to check the response and assumed it was just a standard HTTP response code! – Jon Story Feb 22 '16 at 13:52

28 Answers28

71

FromBody is a strange attribute in that the input POST values need to be in a specific format for the parameter to be non-null, when it is not a primitive type. (student here)

  1. Try your request with {"name":"John Doe", "age":18, "country":"United States of America"} as the json.
  2. Remove the [FromBody] attribute and try the solution. It should work for non-primitive types. (student)
  3. With the [FromBody] attribute, the other option is to send the values in =Value format, rather than key=value format. This would mean your key value of student should be an empty string...

There are also other options to write a custom model binder for the student class and attribute the parameter with your custom binder.

Liam
  • 27,717
  • 28
  • 128
  • 190
Raja Nadar
  • 9,409
  • 2
  • 32
  • 41
  • 4
    removed [FromBody], still not working. content of the student object still null.. (e.g. name=null, country=null, age=null) – InnovativeDan Mar 30 '14 at 09:57
  • IT WORKED! didn't expect the problem to be with my JSON format. THANKS MAN! marked as answer. – InnovativeDan Mar 30 '14 at 10:10
  • I had success with with using the json as you suggestion in #1. Very helpful. – itsmatt Aug 01 '15 at 03:15
  • I was mssing "," in json object, and is even working with [FromBody]. No Issue – Rahul Sonone Jun 28 '16 at 12:43
  • 1
    #1 worked for me. Thank you :) I just needed to remove the 'key' from my JSON and send the 'value' – IsolatedStorage Nov 10 '16 at 02:17
  • 16
    for other readers.. the original problem here was the OP was sending a JSON object which contained a student property, instead of sending just the student object as the webapi was expecting. – Shaun Wilson Dec 09 '16 at 19:26
  • 3
    It's a late comment but your solution helped me fixed the issue I was struggling with from past two hours. Cheers :) – Sahil Sharma Jun 30 '17 at 08:58
  • This post above is a great explanation of the effects of [FromBody] and explains how we shouldn't use it when posting JSON data to serialize in as a parameter, but to leave out [FromBody]. – justdan23 Jun 07 '18 at 21:02
  • @ShaunWilson Your comment is important enough to modify the original answer as such. – P.Brian.Mackey Sep 10 '21 at 16:51
65

I was looking for a solution to my problem for some minutes now, so I'll share my solution.

When you have a custom constructor within your model, your model also needs to have an empty/default constructor. Otherwise the model can't be created, obviously. Be careful while refactoring.

chrjs
  • 2,375
  • 1
  • 25
  • 41
  • 2
    That's an essential info that should be added to the accepted answer. – Youkko Apr 08 '19 at 19:18
  • 1
    this is simply not accurate. Classes ( a model is a class ) have a default constructor, even when not specified and can definitely be created – Andrei Dragotoniu Aug 10 '19 at 07:11
  • I agree with @AndreiDragotoniu. This doesn't seem to be true. I was running into this problem and my issue was from the fact that I had readonly properties. After adding setters to each it worked fine (even with specifying only a non-empty constructor). – Broots Waymb Oct 03 '19 at 14:43
  • 3
    @AndreiDragotoniu I think the problem arises when you have created a constructor which requires arguments. If you have done so this overrides the default constructor. In this case you also need to create an additional empty constructor. – LairdPleng Apr 28 '20 at 04:46
51

I spend several hours with this issue... :( Getters and setters are REQUIRED in POST parameters object declaration. I do not recommend using simple data objects (string,int, ...) as they require special request format.

[HttpPost]
public HttpResponseMessage PostProcedure(EdiconLogFilter filter){
...
}

Does not work when:

public class EdiconLogFilter
{
    public string fClientName;
    public string fUserName;
    public string fMinutes;
    public string fLogDate;
}

Works fine when:

public class EdiconLogFilter
{
    public string fClientName { get; set; }
    public string fUserName { get; set; }
    public string fMinutes { get; set; }
    public string fLogDate { get; set; }
}
Milan Švec
  • 1,675
  • 17
  • 21
  • 4
    Oh wow, I can't believe this was actually my issue... Thanks heaps. – Chris Owens Jan 27 '17 at 07:14
  • Needing Properties with getter and setter fixed my issue for my GET request, what an annoying issue – Mart10 Feb 21 '18 at 13:44
  • Thank you! If I hadn't read your answer I wouldn't have bothered double-checking and noticed that the access modifiers on my DTO's set properties were `internal` and that was causing the problem. – Simon Morgan Feb 10 '19 at 15:36
  • Wow, this was also my solution. People, beware of private setters! Replacing "private set;" to "set;" fixed my issue. – FBaez51 Dec 21 '19 at 13:30
  • Thank you so much! I spent a long time thinking that the JSON or the controllers were wrong and I didn't even think about the object class! – Bennett Forkner Feb 07 '22 at 22:21
  • In 2023 still happening, so glad to arrive at your answer after several hours of desperation – Oscar Hermosilla Jun 28 '23 at 15:46
28

If the any of values of the request's JSON object are not the same type as expected by the service then the [FromBody] argument will be null.

For example, if the age property in the json had a float value:

"age":18.0

but the API service expects it to be an int

"age":18

then student will be null. (No error messages will be sent in the response unless no null reference check).

Ray
  • 187,153
  • 97
  • 222
  • 204
  • Thank you very much! We were trying to bind a complex object to a string (god knows why). After fixing it it worked. This issue was driving me insane. What a stupid decision to fail silently and return `null `... – Lucas Bragança Nov 09 '20 at 17:38
  • hello, any resources that point out how can you locate which property is not the same type as expected ? because for complex objects with lots of child object it's hard to locate the property which is not of the correct type – guxxo Mar 31 '22 at 14:41
  • found the answer to my ^question here: https://stackoverflow.com/a/47562510/7994403 – guxxo Mar 31 '22 at 16:10
15

This is a little old one and my answer will go down to the last place but even so I would like to share my experience.

Tried every suggestion but still having the same "null" value in a PUT [FromBody].

Finally found it was all about Date format while JSON serializing the EndDate property of my Angular Object.

No error was thrown, just received an empty FromBody object....

JoeCool
  • 907
  • 1
  • 11
  • 25
  • 3
    I'd been passing all sorts of odd data to it, expecting it to be clever in it's mapping, but no... It dies and gives you nothing but null. I tried again with well behaved data and hey presto, sorted. Yours was the answer that encouraged me to do this! – MGDavies Jan 15 '17 at 21:02
  • 2
    This answer was very useful as the date formatting was the cause of an error I was running into as well. – Elijah Lofgren Jun 14 '17 at 16:24
  • 1
    Avoid using [FromBody] in front of parameters when they are passed up as JSON within the Body of a POST request. Also, make sure the top level name in your JSON matches the variable name of your parameter. – justdan23 Jun 08 '18 at 19:52
  • 1
    Thanks, You are the real god. – Hirofumi Okino Jan 31 '19 at 18:26
13

If using Postman, make sure that:

  • You have set a "Content-Type" header to "application/json"
  • You are sending the body as "raw"
  • You don't need to specify the parameter name anywhere if you are using [FromBody]

I was stupidly trying to send my JSON as form data, duh...

John M
  • 2,510
  • 6
  • 23
  • 31
  • How do you format your body then ? I mean, if I just put "=This is my value" in the body (declared as a JSON), this will just tell me that the JSON is erroneous. Care to give a small example ? EDIT : Nevermind, just found out the solution. In my case, the "Content-Type" had to be "application/x-www-form-urlencoded" and my body is "raw" of type Text. – Jacks Jul 19 '17 at 10:34
  • If you do NOT use [FromBody], then it should parse your JSON as long as the top level name in your JSON matches the variable name of your parameter. – justdan23 Jun 08 '18 at 20:10
12

UPDATE: A practical solution is writing a custom JSON formatter. For a general description of the problem (but no practical solution), read on here.

TL;DR: Don't use [FromBody], but roll your own version of it with better error handling. Reasons given below.

Other answers describe many possible causes of this problem. However, the root cause is that [FromBody] simply has terrible error handling, which makes it almost useless in production code.

For example, one of the most typical reasons for the parameter to be null is that the request body has invalid syntax (e.g., invalid JSON). In this case, a reasonable API would return 400 BAD REQUEST, and a reasonable web framework would do this automatically. However, ASP.NET Web API is not reasonable in this regard. It simply sets the parameter to null, and the request handler then needs "manual" code to check if the parameter is null.

Many of the answers given here are therefore incomplete with regards to error handling, and a buggy or malicious client may cause unexpected behavior on the server side by sending an invalid request, which will (in the best case) throw a NullReferenceException somewhere and return an incorrect status of 500 INTERNAL SERVER ERROR or, worse, do something unexpected or crash or expose a security vulnerability.

A proper solution would be to write a custom "[FromBody]" attribute which does proper error handling and returns proper status codes, ideally with some diagnostic information to aid client developers.

A solution that might help (not tested yet) is to make parameters required, as follows: https://stackoverflow.com/a/19322688/2279059

The following clumsy solution also works:

// BAD EXAMPLE, but may work reasonably well for "internal" APIs...

public HttpResponseMessage MyAction([FromBody] JObject json)
{
  // Check if JSON from request body has been parsed correctly
  if (json == null) {
    var response = new HttpResponseMessage(HttpStatusCode.BadRequest) {
      ReasonPhrase = "Invalid JSON"
    };
    throw new HttpResponseException(response);
  }

  MyParameterModel param;
  try {
    param = json.ToObject<MyParameterModel>();
  }
  catch (JsonException e) {
    var response = new HttpResponseMessage(HttpStatusCode.BadRequest) {
      ReasonPhrase = String.Format("Invalid parameter: {0}", e.Message)
    };
    throw new HttpResponseException(response);
  }

  // ... Request handling goes here ...

}

This does (hopefully) proper error handling, but is less declarative. If, for example, you use Swagger to document your API, it will not know the parameter type, which means you need to find some manual workaround to document your parameters. This is just to illustrate what [FromBody] should be doing.

EDIT: A less clumsy solution is to check ModelState: https://stackoverflow.com/a/38515689/2279059

EDIT: It appears that ModelState.IsValid is not, as one would expect, set to false if using JsonProperty with Required = Required.Always and a parameter is missing. So this is also useless.

However, in my opinion, any solution that requires writing additional code in every request handler is unacceptable. In a language like .NET, with powerful serialization capabilities, and in a framework like ASP.NET Web API, request validation should be automatic and built-in, and it is totally doable, even though Microsoft does not provide the necessary built-in tools.

Florian Winter
  • 4,750
  • 1
  • 44
  • 69
  • Yes, adding the code referred above what exactly what I was looking for! Add code to check the ModelState: https://stackoverflow.com/a/38515689/2279059 – Rob Bramhall Nov 01 '18 at 15:01
  • @RobBramhall If you use DataContract and not Newtonsoft.Json attributes, then using ModelState is probably fine. – Florian Winter Nov 02 '18 at 16:24
8

It can be helpful to add TRACING to the json serializer so you can see what's up when things go wrong.

Define an ITraceWriter implementation to show their debug output like:

class TraceWriter : Newtonsoft.Json.Serialization.ITraceWriter
{
    public TraceLevel LevelFilter {
        get {
            return TraceLevel.Error;
        }
    }

    public void Trace(TraceLevel level, string message, Exception ex)
    {
        Console.WriteLine("JSON {0} {1}: {2}", level, message, ex);
    }
}

Then in your WebApiConfig do:

    config.Formatters.JsonFormatter.SerializerSettings.TraceWriter = new TraceWriter();

(maybe wrap it in an #if DEBUG)

Phuo
  • 221
  • 2
  • 5
  • thank you bro, this has helped me locate which property from the request's JSON object is not the same type as expected by the API, which results in the [FromBody] argument being null. Really helpful when you have complex [FromBody] objects which contain many child objects. – guxxo Mar 31 '22 at 16:09
6

I was also trying to use the [FromBody], however, I was trying to populate a string variable because the input will be changing and I just need to pass it along to a backend service but this was always null

Post([FromBody]string Input]) 

So I changed the method signature to use a dynamic class and then convert that to string

Post(dynamic DynamicClass)
{
   string Input = JsonConvert.SerializeObject(DynamicClass);

This works well.

Mike
  • 101
  • 1
  • 6
4

After Three days of searching and none of above solutions worked for me , I found another approach to this problem in this Link: HttpRequestMessage

I used one of the solutions in this site

[HttpPost]
public async System.Threading.Tasks.Task<string> Post(HttpRequestMessage request)
{
    string body = await request.Content.ReadAsStringAsync();
    return body;
}
  • Good lords. This was 2 days headbanging in here. Thank You very much! – Yordan Yanakiev Jul 30 '19 at 13:09
  • This doesn't make sense why only HttpRequestMessage param works, my Controller has another method using ([FromBody] ICollection param) and it works perfectly, just like all my other controllers do. And the action method that started giving me this problem was working fine when I first implemented it a few days ago, and the difference is that a .NET Standard which references a .net framework project is calling this web API method, so I'm wondering if this is the "unknown territory" that you end up in if .net standard references .net framework? I just don't want to re-do the project. – Ninos Aug 12 '19 at 20:25
3

Just to add my history to this thread. My model:

public class UserProfileResource
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }

    public UserProfileResource()
    {
    }
}

The above object couldn't be serialized in my API Controller and would always return null. The issue was with Id of type Guid: everytime I passed empty string as an Id (being naive that it will automatically be converted to Guid.Empty) from my frontend I received null object as [FromBody] paramether.

Solution was either to

  • pass valid Guid value
  • or change Guid to String
Mihir Kale
  • 1,028
  • 1
  • 12
  • 20
Dariusz
  • 15,573
  • 9
  • 52
  • 68
2

In my case the problem was the DateTime object I was sending. I created a DateTime with "yyyy-MM-dd", and the DateTime that was required by the object I was mapping to needed "HH-mm-ss" aswell. So appending "00-00" solved the problem (the full item was null because of this).

jwg
  • 5,547
  • 3
  • 43
  • 57
shocks
  • 53
  • 8
2

I've hit this problem so many times, but actually, it's quite straightforward to track down the cause.

Here's today's example. I was calling my POST service with an AccountRequest object, but when I put a breakpoint at the start of this function, the parameter value was always null. But why ?!

[ProducesResponseType(typeof(DocumentInfo[]), 201)]
[HttpPost]
public async Task<IActionResult> Post([FromBody] AccountRequest accountRequest)
{
    //  At this point...  accountRequest is null... but why ?!

    //  ... other code ... 
}

To identify the problem, change the parameter type to string, add a line to get JSON.Net to deserialize the object into the type you were expecting, and put a breakpoint on this line:

[ProducesResponseType(typeof(DocumentInfo[]), 201)]
[HttpPost]
public async Task<IActionResult> Post([FromBody] string ar)
{
    //  Put a breakpoint on the following line... what is the value of "ar" ?
    AccountRequest accountRequest = JsonConvert.DeserializeObject<AccountRequest>(ar);

    //  ... other code ...
}

Now, when you try this, if the parameter is still blank or null, then you simply aren't calling the service properly.

However, if the string does contain a value, then the DeserializeObject should point you towards the cause of the problem, and should also fail to convert your string into your desired format. But with the raw (string) data which it's trying to deserialize, you should now be able to see what's wrong with your parameter value.

(In my case, we were calling the service with an AccountRequest object which had been accidentally serialized twice !)

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
2

This is another issue related to invalid property values in an Angular Typescript request.

This is was related to the conversion between a Typescript number to an int(Int32) in C#. I was using Ticks (UTC milliseconds) which is larger than the signed, Int32 range (int in C#). Changed the C# model from int to long and everything worked fine.

1

I had the same problem.

In my case, the problem was in public int? CreditLimitBasedOn { get; set; } property I had.

my JSON had the value "CreditLimitBasedOn":true when It should contain an integer. This property prevented the whole object being deserialized on my api method.

1

If this is because Web API 2 ran into a deserialization problem due to mismatched data types, it's possible to find out where it failed by inspecting the content stream. It will read up until it hits an error, so if you read the content as a string, you should have the back half of the data you posted:

string json = await Request.Content.ReadAsStringAsync();

Fix that parameter, and it should make it further next time (or succeed if you're lucky!)...

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
Jereme
  • 1,445
  • 1
  • 16
  • 32
  • Request.Content.ReadAsStringAsync will get affected if your service gets 1000+ requests per second then string get null. we need to handle this. – Amol Shiledar Jul 30 '18 at 14:51
1

Maybe for someone it will be helpful: check the access modifiers for your DTO/Model class' properties, they should be public. In my case during refactoring domain object internals were moved to DTO like this:

// Domain object
public class MyDomainObject {
    public string Name { get; internal set; }
    public string Info { get; internal set; }
}
// DTO
public class MyDomainObjectDto {
    public Name { get; internal set; } // <-- The problem is in setter access modifier (and carelessly performed refactoring).
    public string Info { get; internal set; }
}

DTO is being finely passed to client, but when the time comes to pass the object back to the server it had only empty fields (null/default value). Removing "internal" puts things in order, allowing deserialization mechanizm to write object's properties.

public class MyDomainObjectDto {
    public Name { get; set; }
    public string Info { get; set; }
}
ddolgushin
  • 11
  • 1
  • **this** was my _problem_ Had declared members `internal` for my bad, and resolved it after two days.. – Irf Mar 24 '20 at 19:07
1

Check if JsonProperty attribute is set on the fields that come as null - it could be that they are mapped to different json property-names.

Bashir Magomedov
  • 2,841
  • 4
  • 28
  • 44
  • This was the problem for me as well. I also noticed that I could decorate my properties with [JsonProperty("myProperty")] *or* I could use the `[DataMember]` attribute to get my properties values to propagate. – Dan Beaulieu Jun 21 '18 at 12:56
1

I used HttpRequestMessage and my problem got solved after doing so much research

[HttpPost]
public HttpResponseMessage PostProcedure(HttpRequestMessage req){
...
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
1

In my case, using postman I was sending a DateTime with invalid separators (%) so the parse failed silently. Be sure you are passing valid params to your class constructor.

Sidonai
  • 3,088
  • 3
  • 13
  • 12
1

None of the above was my solution: in my case the issue is that [ApiController] was not added to the controller so it is giving Null value

[Produces("application/json")]
[Route("api/[controller]")]
[ApiController] // This was my problem, make sure that it is there!
public class OrderController : Controller

...

Adel
  • 1,468
  • 15
  • 18
  • What about inheriting from `ApiController` itself? – Zimano Feb 03 '20 at 21:17
  • Check your json structure, might have invalid json, example past your input throught https://jsonformatter.curiousconcept.com/ – Adel Feb 04 '20 at 05:39
  • If you were responding to me: I meant that your answer suggests adding the `ApiController` as a class attribute, while the class is inheriting from `Controller`; I believe you can achieve the same result by inheriting from `ApiController` directly, which is done by default in the Controllers generated by a WebAPI project in VS. – Zimano Feb 04 '20 at 18:26
1

In my case (.NET Core 3.0) I had to configure JSON serialization to resolve camelCase properties using AddNewtonsoftJson():

services.AddMvc(options =>
{
    // (Irrelevant for the answer)
})
.AddNewtonsoftJson(options =>
{
    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});

Do this in your Startup / Dependency Injection setup.

user1987392
  • 3,921
  • 4
  • 34
  • 59
1

I just ran into this and was frustrating. My setup: The header was set to Content-Type: application/JSON and was passing the info from the body with JSON format, and was reading [FromBody] on the controller.

Everything was set up fine and I expect it to work, but the problem was with the JSON sent over. Since it was a complex structure, one of my classes which was defined 'Abstract' was not getting initialized and hence the values weren't assigned to the model properly. I removed the abstract keyword and it just worked..!!!

One tip, the way I could figure this out was to send data in parts to my controller and check when it becomes null... since it was a complex model I was appending one model at a time to my request params. Hope it helps someone who runs into this stupid issue.

chethandb
  • 1,021
  • 1
  • 9
  • 17
0

Seems like there can be many different causes of this problem...

I found that adding an OnDeserialized callback to the model class caused the parameter to always be null. Exact reason unknown.

using System.Runtime.Serialization;

// Validate request
[OnDeserialized]  // TODO: Causes parameter to be null
public void DoAdditionalValidatation() {...}
Florian Winter
  • 4,750
  • 1
  • 44
  • 69
0

I had this problem in my .NET Framework Web API, because my model existed in a .NET Standard project that referenced a different version of data annotations.

Adding the ReadAsAsync line below highlighted the cause for me:

public async Task<HttpResponseMessage> Register(RegistrationDetails registrationDetails)
{
    var regDetails = await Request.Content.ReadAsAsync<RegistrationDetails>();
Craig
  • 417
  • 3
  • 12
0

I was struggling with this for hours today. I could see that there was data in the response body because of the length, but any time I tried to read the data, I got an empty string, or the arguments in the parameter list for the method returned null. I compared this controller to another that was already working and found that I was missing the ApiController attribute for the class declaration. I also removed the FromBody attribute from my parameter declaration. I am not sure when that was added, but I am using .Net 5.0.

fizch
  • 2,599
  • 3
  • 30
  • 45
0

As detailed in my other answer, the problem is with error handling in the [FromBody] attribute, and you cannot do much about that without writing your own version of it.

However, a general solution that will improve error handling in your entire API without making changes in any controller or action is to write a custom JSON formatter (derived from FotoWareApiJsonFormatter) which handles serialization errors properly.

I will not present the entire solution here, but the important part is to catch JsonSerializationException and JsonReaderException in the formatter and make sure the endpoint will return 400 Bad Request as a result.

This ensures that if the request contains invalid JSON, or the JSON does not fulfill model constraints (such as missing required properties, type errors, etc.), the API will automatically return 400 Bad Request before your controller action is called, so you do not need to write extra error handling in the controller, and your parameter using [FromBody] will never be null.

// in JSON formatter class
private object Deserialize(Stream readStream, Type type)
{
    try
    {
        var streamReader = new StreamReader(readStream);
        return GetSerializer().Deserialize(streamReader, type);
    }
    catch (JsonSerializationException e)
    {
        // throw another exception which results in returning 400 Bad Request
    }
    catch (JsonReaderException e)
    {
        // throw another exception which results in returning 400 Bad Request
    }
}

You also have to ensure that your custom JSON formatter is the only formatter, e.g., by adding this code to Application_Start():

var jsonFormatter = new MyJsonFormatter();

// Always return JSON, and accept JSON only
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(jsonFormatter);

This assumes that your API accepts and returns JSON only, as most modern APIs do. Offering XML or other formats as an alternative if you are not going to test or advertise it is unnecessary at best and a potential security risk at worst.

Be careful when introducing this to an existing API, as it can introduce some unexpected breaking changes, so good testing is advised. Consider it as a cleanup of the error handling in your API.

Florian Winter
  • 4,750
  • 1
  • 44
  • 69
-1

Just one more thing to look at... my model was marked as [Serializable] and that was causing the failure.