I am working on a WCF rest based services. I have written Get and Post methods in my service and Get methods are able to working (fetching data) when I typed in URL (in JSON
Finally after many trials and many things, I got the working solution. Here I am posting what I did for the POST method to work.
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "AddOrders", RequestFormat =
WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,BodyStyle =
MessageBodyStyle.Bare)]
int AddOrders(RequestData orderRequestData);
This is for implementation in the operation contract.
Client application:-
WebClient WC = new WebClient();
WC.Headers.Add("Content-Type", "application/json");
WC.Encoding = Encoding.UTF8;
MemoryStream MS = new MemoryStream();
DataContractJsonSerializer JSrz = new
DataContractJsonSerializer(typeof(RequestData));
JSrz.WriteObject(MS, order);
string data = Encoding.UTF8.GetString(MS.ToArray(), 0, (int)MS.Length);
byte[] res1 =
WC.UploadData("http://localhost/EMCService/Service2.svc/AddOrders", "POST",MS.ToArray());
MS = new MemoryStream(res1);
JSrz = new DataContractJsonSerializer(typeof(int));
int result = (int)JSrz.ReadObject(MS);
I did not made any config settings and still using the old settings of web.config what I have posted in the above question, and it is working.