333Matt
333Matt

Reputation: 1188

How to diagnose malformed JObject in ASP.NET WebAPI?

I wrote the following method to try to diagnose a problem I'm experiencing in my application:

    [Route("/api/flow/test"), HttpPost]
    [ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]
    public async Task<IActionResult> Test(string id, [FromBody] JToken input)
    {            
        var result = input == null ? "Not OK" : "OK";
        return Ok(result);
    }

I post a large (6.5MB+) JSON body to it, and in one instance it works fine. When I post a similar JSON with some added properties, it does not - the input parameter comes in as null. However, both JSON validate successfully with every tool I've found that can handle their size. Please provide some additional ideas about how to further investigate what is causing the body parameter input to be treated as null.

Upvotes: 0

Views: 42

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174397

One option would be to declare it as string instead of JToken and try to explicitly parse it in the action body.

This will show you two things:

  1. If input is still empty, it's not a problem with JSON parsing
  2. If parsing it explicitly fails, the JSON is indeed invalid.

Only when input is not null and parsing it in the body works - only then you need to deep dive into what actually happens, when you declare an action parameter as JToken.

Upvotes: 3

Related Questions