We have an MVC application (MVC4) that can sometimes send JSON events sent by a third party to our specific URL (" http://server.com/events/ "). The JSON event is in the body of the HTTP POST, and the body is strictly JSON ( Content-Type: application/json is not a post form with JSON in some string field).
How can I get the JSON body inside the controller body? I tried the following but got nothing
[Edit] : When I said that I received nothing, I meant that jsonBody is always null, regardless of whether I define it as Object or string .
[HttpPost] // this maps to http://server.com/events/ // why is jsonBody always null ?! public ActionResult Index(int? id, string jsonBody) { // Do stuff here }
Note that I know that if I declare a method with a strongly typed input parameter, MVC does all the analysis and filtering, i.e.
// this tested to work, jsonBody has valid json data // that I can deserialize using JSON.net public ActionResult Index(int? id, ClassType847 jsonBody) { ... }
However, the JSON that we get is very diverse, so we donβt want to define (and maintain) hundreds of different classes for each variant of JSON.
I check this with the following curl command (with one JSON option here)
curl -i -H "Host: localhost" -H "Content-Type: application/json" -X POST http://localhost/events/ -d '{ "created": 1326853478, "data": { "object": { "num_of_errors": 123, "fail_count": 3 }}}
json post asp.net-mvc
DeepSpace101 Oct 24 2018-12-12T00: 00Z
source share