web api - FromBody Value cannot be null

Created: 17/10/2018 10:00:20 p.m. web api - [FromBody] Value cannot be null

1) When you call your api from, for example Restlet Client you get the following error:

Value cannot be null


"Message": "An error has occurred.",
"ExceptionMessage": "Value cannot be null.\r\nParameter name: value",
"ExceptionType": "System.ArgumentNullException",
"StackTrace": " at Newtonsoft.Json.Utilities.ValidationUtils.ArgumentNotNull(Object
 value, String parameterName)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObjec
t(String value, Type type, JsonSerializerSettings settings)\r\n at Newtonsoft.Jso
n.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)\r\n at M
vcApplication1.Controllers.PersonsController.Post(String value) in c:\\Users\
\mak\\Documents\\Visual Studio 2012\\Projects\\MvcApplication1\\MvcApplicati
on1\\Controllers\\PersonsController.cs:line 42\r\n at lambda_method(Closure
 , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActi
onDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object ins
tance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.Reflected
HttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments
)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__Displa
yClass5.<ExecuteAsync>b__4()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchr
onously[TResult](Func`1 func, CancellationToken cancellationToken)"

2) Restlet result

3) That's because you are sending pure json object and not, simple string json, as your method expect
just add double quotes to your json object and you will be good.

4) Final result, your json was parsed

Of course the best option is to recieve direcly an object insted of a json string.
So it will be like [FromBody] MyObjectXyz value
and then you dont need to deserelize anything, json will be mappen automatically to your object properties. Finally when you make your request, in the body you will send json not string with json.



Go to Comments