Home > Back-end >  Why isn't my API controller catching bad data in my if statement?
Why isn't my API controller catching bad data in my if statement?

Time:12-20

I have logic like this in my .Net Core API controller that checks for bad values sent by a front-end:

[HttpPut("{id}")]
public async Task<IActionResult> PutMathCoordinates(MathCoordinates)

if ((MathCoordinates.GraphX == 0) && (MathCoordinates.GraphY == 0))
{
    // send message that data is bad
}

But even with that, I get some 400 errors like this:

400 The JSON value could not be converted to System.Guid

When the front-end sends empty data like this:

{problemId: "", mathId: "", graphX: 0, graphY: 0}

Why isn't my controller catching that in my if statement?

Thanks!

CodePudding user response:

  1. Your input for the method is missing a declaration, it should be something like MathCoordinates input which you then refer as input.GraphX
  2. You're trying to post a GUID to a method requiring an object of type MathCoordinates. Either you need to change the input parameters and fetch the model yourself based on the GUID parameter, or post JSON representing the body so JSON can automatically serialize the JSON to the model type (requires [FromBody] on the input).
  • Related