Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC controller : get JSON object from HTTP body?

We have an MVC (MVC4) application which at times might get a JSON events POSTed from a 3rd 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 - not a form-post with JSON in some string field).

How can I receive the JSON body inside the controller's body? I tried the following but didn't get anything

[Edit]: When I say didn't get anything 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 if I give declare the method with a strongly typed input parameter, MVC does the whole parsing 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 we get is very varied, so we don't want to define (and maintain) hundreds of different classes for each JSON variant.

I'm testing this by the following curl command (with one variant of the JSON 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 }}} 
like image 662
DeepSpace101 Avatar asked Oct 24 '12 01:10

DeepSpace101


2 Answers

use Request.Form to get the Data

Controller:

    [HttpPost]     public ActionResult Index(int? id)     {         string jsonData= Request.Form[0]; // The data from the POST     } 

I write this to try

View:

<input type="button" value="post" id="btnPost" />  <script type="text/javascript">     $(function () {         var test = {             number: 456,             name: "Ryu"         }         $("#btnPost").click(function () {             $.post('@Url.Action("Index", "Home")', JSON.stringify(test));         });     }); </script> 

and write Request.Form[0] or Request.Params[0] in controller can get the data.

I don't write <form> tag in view.

like image 44
ChunHao Tang Avatar answered Sep 17 '22 18:09

ChunHao Tang


It seems that if

  • Content-Type: application/json and
  • if POST body isn't tightly bound to controller's input object class

Then MVC doesn't really bind the POST body to any particular class. Nor can you just fetch the POST body as a param of the ActionResult (suggested in another answer). Fair enough. You need to fetch it from the request stream yourself and process it.

[HttpPost] public ActionResult Index(int? id) {     Stream req = Request.InputStream;     req.Seek(0, System.IO.SeekOrigin.Begin);     string json = new StreamReader(req).ReadToEnd();      InputClass input = null;     try     {         // assuming JSON.net/Newtonsoft library from http://json.codeplex.com/         input = JsonConvert.DeserializeObject<InputClass>(json)     }      catch (Exception ex)     {         // Try and handle malformed POST body         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);     }      //do stuff  } 

Update:

for Asp.Net Core, you have to add [FromBody] attrib beside your param name in your controller action for complex JSON data types:

[HttpPost] public ActionResult JsonAction([FromBody]Customer c) 

Also, if you want to access the request body as string to parse it yourself, you shall use Request.Body instead of Request.InputStream:

Stream req = Request.Body; req.Seek(0, System.IO.SeekOrigin.Begin); string json = new StreamReader(req).ReadToEnd(); 
like image 63
DeepSpace101 Avatar answered Sep 21 '22 18:09

DeepSpace101