Search code examples
asp.netjsonasp.net-web-apizendesk

WebApi Accepting Json Issues - ZenDesk


I have the following Asp.Net Web Api v2 controller - just as a basic example:

    [HttpPost]
    public IHttpActionResult TestPost([FromBody]string value)
    {       
       l.TestNotify(value, "test");     
       return Ok();
    }

When i pass a JSON request to the controller with a a simple "Hello world" - i am able to get the value no problem. When i use more complicated JSON, the value parameter returns empty.

I have tested the same JSON with a test program using restsharp and it works properly.

Here is the request:

Content-Type    application/json; charset=utf-8
Accept-Encoding gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept  */*
Content-Length  743

Here is the Json Snippet:

{
  "ticket": {
    "url": "",
    "id": "",
    "external_id": null,
    "FromEmail": "cody",
    "via": "Mail",
  }
}

My guess is that it has something to do between the request differences but nothing obvious comes to mind. Other methods for reading request content or an adjustment in my understanding about the http request above would be appreciated.


Solution

  • Don't use string, rather create a class for the input type.

    So this is how the class should look like:

    public class Ticket
    {
        public string url { get; set; }
        public string id { get; set; }
        public object external_id { get; set; }
        public string FromEmail { get; set; }
        public string via { get; set; }
    }
    
    public class TicketRoot
    {
        public Ticket ticket { get; set; }
    }
    

    Then use the TicketRoot type instead of string from your controller.