title: MVC controller : get JSON object from HTTP body?
body: <p>We have an MVC (MVC4) application which at times might get a JSON events POSTed from a 3rd party to our specific URL ("<a href="http://server.com/events/" rel="noreferrer">http://server.com/events/</a>"). The JSON event is in the body of the HTTP POST and the body is strictly JSON (<code>Content-Type: application/json</code> - not a form-post with JSON in some string field).</p>
  
  <p>How can I receive the JSON body inside the controller's body? I tried the following but didn't get anything</p>
  
  <p><strong>[Edit]</strong>: When I say <em>didn't get anything</em> I meant that jsonBody is always null regardless of whether I define it as <code>Object</code> or <code>string</code>. </p>
  
  <pre><code>[HttpPost]
  // this maps to http://server.com/events/
  // why is jsonBody always null ?!
  public ActionResult Index(int? id, string jsonBody)
  {
      // Do stuff here
  }
  </code></pre>
  
  <p>Note that I know if I give declare the method with a strongly typed input parameter, MVC does the whole parsing and filtering i.e.</p>
  
  <pre><code>// this tested to work, jsonBody has valid json data 
  // that I can deserialize using JSON.net
  public ActionResult Index(int? id, ClassType847 jsonBody) { ... }
  </code></pre>
  
  <p>However, the JSON we get is very varied, so we don't want to define (and maintain) hundreds of different classes for each JSON variant.</p>
  
  <p>I'm testing this by the following <code>curl</code> command (with one variant of the JSON here)</p>
  
  <pre><code>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 }}}
  </code></pre>