1

I have read through this thread and none of the answers have been able to solve my issue.

I am attempting to post a simple POCO to a .Net Web API controller but no matter what is in the body, the parameter is always a default object, never populated.

My SimplePOCO object:

[Serializable]
public class SimplePOCO
{
    public SimplePOCO() {}
    public int MyProperty { get; set; }
    public string MyProperty2 { get; set; }
}

I am using RestSharp client to post to the controller using the following code:

public void PostSimplePOCO(SimplePOCO simplePOCO)
{
    var request = new RestRequest("api/Warehouse/SimplePOCO", Method.POST);
    request.AddJsonBody(simplePOCO);
    IRestResponse response = Execute(request);
}

My Api controller method:

[HttpPost]
[Route("api/Warehouse/SimplePOCO")]
public void ImportPOCO([FromBody]SimplePOCO simplePOCO)
{
    using (var stream = new MemoryStream())
    {
        var context = (HttpContextBase)Request.Properties["MS_HttpContext"];
        context.Request.InputStream.Seek(0, SeekOrigin.Begin);
        context.Request.InputStream.CopyTo(stream);
        string requestBody = Encoding.UTF8.GetString(stream.ToArray());
    }
}

I have tried with and without the [FromBody] attribute. I've also tried using type JObject as the parameter which I can then successfully deserialize into the SimplePoco object. If I check the requestBody I can see that the JSON being received is valid, as shown below:

{"MyProperty":2,"MyProperty2":"Test string"}

In addition, I have turned on diagnostic logging and the Output windows says that the model is valid:

w3wp.exe Information: 0 : Request, Method=POST, Url=http://lewis-10:8765/api/Warehouse/SimplePOCO, Message='http://lewis-10:8765/api/Warehouse/SimplePOCO' w3wp.exe Information: 0 : Message='Warehouse', Operation=DefaultHttpControllerSelector.SelectController w3wp.exe Information: 0 : Message='DMHWebAPI.Controllers.WarehouseController', Operation=DefaultHttpControllerActivator.Create w3wp.exe Information: 0 : Message='DMHWebAPI.Controllers.WarehouseController', Operation=HttpControllerDescriptor.CreateController w3wp.exe Information: 0 : Message='Selected action 'ImportPOCO(SimplePOCO simplePOCO)'', Operation=ApiControllerActionSelector.SelectAction w3wp.exe Information: 0 : Message='The authentication filter successfully set a principal to a known identity. Identity.Name=''. Identity.AuthenticationType='Bearer'.', Operation=HostAuthenticationFilter.AuthenticateAsync w3wp.exe Information: 0 : Operation=HostAuthenticationFilter.ChallengeAsync w3wp.exe Information: 0 : Operation=AuthorizeAttribute.OnAuthorizationAsync w3wp.exe Information: 0 : Message='Value read='DMH.DTO.Upload.SimplePOCO'', Operation=JsonMediaTypeFormatter.ReadFromStreamAsync w3wp.exe Information: 0 : Message='Parameter 'simplePOCO' bound to the value 'DMH.DTO.Upload.SimplePOCO'', Operation=FormatterParameterBinding.ExecuteBindingAsync w3wp.exe Information: 0 : Message='Model state is valid. Values: simplePOCO=DMH.DTO.Upload.SimplePOCO', Operation=HttpActionBinding.ExecuteBindingAsync

Raw Fiddler request for reference:

POST http://lewis-10:8765/api/Warehouse/SimplePOCO HTTP/1.1
Authorization: Bearer <redacted>
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
User-Agent: RestSharp/106.6.10.0
Content-Type: application/json
Host: <redacted>
Content-Length: 44
Accept-Encoding: gzip, deflate

{"MyProperty":2,"MyProperty2":"Test string"}
Lewis Hamill
  • 171
  • 1
  • 13

0 Answers0