WebAPI don't deserialize JSON but same payload works in Swagger

24 views Asked by At

I have a WebAPI that has been in use for the last 7 years (.NET 4.7), it's been working without a hitch since the first release. Since March 10th, all of a sudden, it simply refuses to deserialize the JSON payload that gets posted to it.

The model looks like this:

[XmlType(AnonymousType = true, Namespace = "http://dataprocessor.com/payload")]
    public class DataReq
    {
        [XmlElement(Form = XmlSchemaForm.Unqualified)]
        public string Name { get; set; }
        [XmlElement(Form = XmlSchemaForm.Unqualified)]
        public string B64Payload { get; set; }
    }

And the controller looks like this:

[HttpPost]
[Route("api/processdata")]
public ResponseObj ProcessReport([FromBody]DataReq payLoad)
{
    using (DataProcessing dp = new DataProcessing())
    {
       ResponseObj response = dp.ProcessData(payLoad);
       return response;
    }
}

I've also set up a function in order to dump the incoming request body which I then write to a file:

private async Task<string> GetRawPostData()
{
    using (var contentStream = await Request.Content.ReadAsStreamAsync())
    {
        contentStream.Seek(0, SeekOrigin.Begin);
        using (var sr = new StreamReader(contentStream))
        {
            return sr.ReadToEnd();
        }
    }
}

But if I post a payload I get something along the lines of this:

{
    "name": "Testpayload",
    "b64Payload": "SGVsbG8gd29ybGQ="
}

I get null from it when debugging it in VS, it simply can't deserialize it by the looks of it. It has, as I said, for the last 7 years, but not anymore.

HOWEVER, if I take the payload that I dumped in the GetRawPostData() and paste it into the Swagger page of the API, it goes through without any problems whatsoever.

I've been struggling with this all day but I simply cannot get my head around this, please help!

1

There are 1 answers

0
JaggenSWE On

Finally found an answer after even more bits and pieces. ASP.NET Web Api - the framework is not converting JSON to object when using Chunked Transfer Encoding

Exactly why this just happened, no idea, but at least it's a fix.