2

I made a fairly simple mistake. In Fiddler, I created a POST. Notice in "FileFullpath", I used a single backslash instead of two.

Fiddler POST

My Web API model is defined like this ...

public class GTMetadataModel
{
    public int Id { get; set; }

    public string ComputerId { get; set; }
    public string UserId { get; set; }
    public string FileFullpath { get; set; }

    public string Version { get; set; }
    public string[] Categories { get; set; }
    public double[] Scores { get; set; }
}

My Web API controller is defined like this...

public HttpResponseMessage PostGTMetadata(GTMetadataModel newentry)
{
    ... // handle null parameter and return error here.
    var response = Request.CreateResponse<GTMetadataModel>(HttpStatusCode.Created, newentry);
    return response;
}

When I run the Web API and send the Fiddler POST, "newentry" is null. It took a little time before I realized I needed two backslashes. I changed it to two and then "newentry" was correct.

So, clearly the problem was bad data being supplied, but where and how in my server side code would I be able to detect the bad Json data?

Update: Mark's answer is accepted. Because the referenced post is a good example of a robust, elegant approach. But, for those who just want the simple answer, the referenced post uses "this.ModelState" to identify the problem with the parsed data. I could add the following code to my Post handler method (more likely, I will adapt something from the answer Mark references).

if(ModelState.IsValid == false)
{
    var errors = ModelState
        .Where(s => s.Value.Errors.Count > 0)
        .Select(s => s.Key + ": " + s.Value.Errors.Select(t=>t.Exception.Message).Aggregate((a,b) => a + "\n" + b))
        .Aggregate((a, b) => a + "\n" + b);
    Debug.Print(errors);
    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, errors);
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Les
  • 10,335
  • 4
  • 40
  • 60
  • Immediately. You can and should validate that newentry is not null and that it passes validation before performing any sensitive actions on it. – David L Feb 06 '15 at 19:06
  • here is something that someone did using RexEx - http://stackoverflow.com/questions/25209757/how-can-i-detect-invalid-json-containing-a-trailing-comma-with-c – MethodMan Feb 06 '15 at 19:06
  • @DavidL - yes, I detect it is null. But newentry is already being parsed by the Web API framework and presumably failing (in order to pass null). I would like to detect the failure there, or at least access it's assessment of the error. – Les Feb 06 '15 at 19:15
  • @Les You are most likely looking to overload the MediaTypeFormatter then: http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters or by applying a filter. See all of the extensibility points: http://www.asp.net/web-api/overview/advanced/configuring-aspnet-web-api – David L Feb 06 '15 at 19:19

1 Answers1

0

You can use an action filter to intercept the bad request.

See the question, Validating parameters in ASP.NET Web API model binder

Community
  • 1
  • 1
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • @ I'm marking this as the answer. I like the solution, but I updated my question to give the gist of the answer, i.e., the use of ModelState. – Les Feb 06 '15 at 20:47