Hope this helps.
Looking in different comments and other forums, I mix some snippets and For me, this code works...
... In the Controller
public HttpResponseMessage Post([FromBody] string jsonData)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, jsonData);
try
{
string jsonString = jsonData.ToString();
JArray jsonVal = JArray.Parse(jsonString) as JArray;
dynamic mylist= jsonVal;
foreach (dynamic myitem in mylist)
{
string strClave=string.Empty;
string strNum=string.Empty;
string strStatus=string.Empty;
strClave = myitem.clave;
strNum=myitem.num;
strStatus = myitem.status;
}
... in the WebApiConfig.cs include this line for avoid the null value in the [FromBody] variable var jsonFormatter = config.Formatters.OfType().First();
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First(); /*this line makes no more null when use [FromBody]*/
}
.... in the client side , the mos important is concatenate the equal sign before the serialized data ( string json = "=" + SerialData; )
for seralize I'm using
System.Web.Script.Serialization.JavaScriptSerializer serializer = new
System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in DsCnx.Tables[0].Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in DsCnx.Tables[0].Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
SerialData= serializer.Serialize(rows);
PostRequest("http://localhost:53922/api/Demo", SerialData);
this is my PostRequest function, here the Content Type thet I use is httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=utf-8";:
private static string PostRequest(string url, string SerialData)
{
string result = String.Empty;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "=" + SerialData;
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
try
{
using (var response = httpWebRequest.GetResponse() as HttpWebResponse)
{
if (httpWebRequest.HaveResponse && response != null)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}
}
}
catch (WebException e)
{
if (e.Response != null)
{
using (var errorResponse = (HttpWebResponse)e.Response)
{
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
{
string error = reader.ReadToEnd();
result = error;
}
}
}
}
return result.ToString();
}
here is the links where i found one code example for reference :
https://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing#jobject-and-jarray-in-aspnet-web-api
https://blog.codenamed.nl/2015/05/12/why-your-frombody-parameter-is-always-null/