Raja sekaran

Raja sekaran

  • NA
  • 5
  • 36.7k

Pass json string data to Rest service (POST Method) using c#

Dec 9 2013 7:00 AM

Hi guys,

  In my project i have to pass dynamic Json string to WCF Rest service. Here both Input / Output should be in Json format. I am using server side code to pass my data. Here is my code 

In testpage.aspx.cs Page
------------------------

    string json = "{\"sjSonData\":[{"
                             + "\"log_Id\" : 0,"
                             + "\"user_Id\" : 1249,"
                             + "\"session_key\" : \"dvnoewcdw\","
                             + "\"app_id\" : 1,"
                             + "\"app_version\" :\"1.2.7\","
                             + "\"app_isOnline\" : true,"
                             + "\"app_dateTimeZone\" : \"1997-07-16T19:20:30+01:00\","
                             + "\"log_typeId\" : 1,"
                             + "\"log_description\" : \"valid\""
                         + "},"
                         + "{"
                             + "\"log_Id\" : 1,"
                             + "\"user_Id\" : 1249,"
                             + "\"session_key\" : \"dvnoewcdw\","
                             + "\"app_id\" : 1,"
                             + "\"app_version\" : \"1.2.7\","
                             + "\"app_isOnline\" : true,"
                             + "\"app_dateTimeZone\" : \"1997-07-16T19:20:30+01:00\""
                             +"}]}";
    
    const string url = "http://localhost/RestServiceImpl.svc/jsoninputdata";
  
           //create an HTTP request to the URL that we need to invoke
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                //request.ContentLength = byteArray.Length;
                request.ContentType = "application/json; charset=utf-8"; //set the content type to JSON
                request.Method = "POST"; //make an HTTP POST

                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    //initiate the request
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    var resToWrite = serializer.Deserialize<Dictionary<string, object>>(json);
                    streamWriter.Write(restoWrite);
                    streamWriter.Flush();
                    streamWriter.Close();
                }

                // Get the response.
                WebResponse response = request.GetResponse();                
                var streamReader = new StreamReader(response.GetResponseStream());                
                var result = streamReader.ReadToEnd();


I am new to WCF service. I don't know how to pass this data to Rest service. Here my input data has been differed every call. 

Rest Service code
-------------------

[ServiceContract]
 public interface IRestServiceImpl
 {
   [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "jsoninputdata")]
    string jsoninputdata(List<Dictionary<string, object>> rData);
 }


 public class RestServiceImpl : IRestServiceImpl
 {

      public jSonResponseData jsoninputdata(List<Dictionary<string, object>> rData)
      {   

          string json = JsonConvert.SerializeObject(rData);         
          

          //Do my stuff here.....

          return ojSonRes; // Here i need to return my output mentioned below
      }
  }




And i need to return data (output) like below,


**Condition 1:** If both saved in db without any error,

{"sjSonData":[{
  "success":true
}]}


**Condition 2:** Suppose any one data failed to save in database then

{"sjSonData":[
{
"log_Id":1,
"success":false
},
{
 "log_Id":2,
 "success":true
}
]}


Now I am getting result like,

{"sjSonData":"{}"}

Am i doing anything wrong,and also please explain what are all changes i have to do in web.config file. 

Answers (5)