I create web service to save data in database
public string SaveData(String resultData)
{
DLR service = new DLR();
Context.Response.ContentType = "application/json";
string filePath = Server.MapPath("~/Error.txt");
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Json is sent :" + resultData +
"" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
RootObject obj = JsonConvert.DeserializeObject<RootObject>(resultData);
foreach (JsonData item in obj.results)
{
{
var connStr = ConfigurationManager.ConnectionStrings["myCon1"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
try
{
SqlCommand cmd = new SqlCommand("[insert_RDL]", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@msgId", item.msgId);
cmd.Parameters.AddWithValue("@to_mobile", item.to);
cmd.Parameters.AddWithValue("@status", item.status);
cmd.Parameters.AddWithValue("@ttest", "");
cmd.Parameters.AddWithValue("@newtest", "");
con.Open();
cmd.ExecuteNonQuery();
}
finally
{
}
}
}
return "OK ";
}
When i post the json by ajax in aspx page or by PHP SoapClient the page is sending json without problem .
But post it by PHP Curl
<?php
$data = '{"results": [{"msgId": "001","to": "9665312114","status": "D"}, {"msgId": "859911880","to": "966535112578","status": "N"}, {"msgId": "859911880","to": "966535112579","status": "S"}]}' ;
$param = array('resultData' => $data);
var_dump($param);
echo "<hr>";
print_r($param);
$headers = array('Accept: application/json','Content-Type: application/json', );
echo "<hr>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, "http://asdm.sa/DLR.asmx/SaveData");
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
$result =curl_exec($ch);
print_r($result);
echo "<hr>";
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
?>
A problem a json is sent or sent NULL
{"Message":"Invalid JSON primitive: --------------------------60620f7a041fa3f6.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
How i can sloved it
Thank you
What I have tried:
I used Postman to create the curl command for reference.
But Same problem >